UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Controlled Length Value Of Control On Form, Access 2003    
 
   
mr.siro
post Feb 11 2019, 02:19 AM
Post#1



Posts: 147
Joined: 27-January 18



Hello guys,
I have backend is SQLserver and some StoreProd with parameters which have different length: nvarchar(10),nvachar(50),.......
In fontend, if user type string have length lagre than length of parameter, they get error.
The way i used to solved is limit length of string when user type.
But, i have many textbox and many parameter. So, It takes a lot of time and effort.
Somebody have a better way, please tell me.
This post has been edited by mr.siro: Feb 11 2019, 02:20 AM
Go to the top of the page
 
cheekybuddha
post Feb 11 2019, 07:01 AM
Post#2


UtterAccess VIP
Posts: 10,880
Joined: 6-December 03
From: Telegraph Hill


One solution might be:

1. In a standard module:
CODE
Function SetupCharLimits(frm As Form, ParamArray ctls() As Variant) As Boolean

  Dim i As Integer
  
  If UBound(ctls) >= 0 Then
    If UBound(ctls) Mod 2 = 1 Then
      For i = 0 To UBound(ctls) Step 2
        frm.Controls(ctls(i)).OnChange = "=LimitChars([" & ctls(i) & "], " & ctls(i + 1) & ")"
      Next i
      SetupCharLimits = (Err = 0)
    Else
      MsgBox "Uneven number of parameter pairs!"
    End If
  End If

End Function

Function LimitChars(txt As TextBox, MaxChars As Integer) As Boolean

  With txt
    If Len(.Text) > MaxChars Then
      MsgBox "You have exceeded the maximum length of " & MaxChars & " characters.", vbOKOnly + vbInformation, "To many characters ..."
      .Text = Left(.Text, MaxChars)
      .SelStart = MaxChars
    Else
      LimitChars = True
    End If
  End With

End Function


Then in each form where you have controls to limit:
CODE
Private Sub Form_Load()

' Parameter 1 = Me - ie this form
' Subsequent parameters must be in pairs: textbox name, then max length - repeat for each control to limit
  Call SetupCharLimits(Me, "Text0", 5, "Text2", 4, "Text3", 10)
  
End Sub


hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
mr.siro
post Feb 11 2019, 07:21 AM
Post#3



Posts: 147
Joined: 27-January 18



hello cheeky,

With your code, user can type 5 characters, after that show msgbox and delete 1 characters.

I want if setting is 4 characters then user only can type 4 characters. How to do that.
Go to the top of the page
 
cheekybuddha
post Feb 11 2019, 07:29 AM
Post#4


UtterAccess VIP
Posts: 10,880
Joined: 6-December 03
From: Telegraph Hill


Look at the SetupCharLimits() function called in Form_Load():

Call SetupCharLimits(Me, "Text0", 5, "Text2", 4, "Text3", 10)

Each pair consists of the name of the textbox control followed by the maximum length it can have.

So:
Text0 has maximum 5 characters
Text2 has maximum 4 characters
Text3 has maximum 10 characters

You put in the appropriate control name and its maximum character count.

hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
mr.siro
post Feb 11 2019, 07:46 AM
Post#5



Posts: 147
Joined: 27-January 18



i meant : Call SetupCharLimits(Me, "Text0", 5). User can type 6 characters, but after that msgbox is show, and one characters was delete.
Now, i want, user only can type 5 characters, don't need msgbox anymore. It's look like used inputmask property: 0000, then user only can type 4 character

This post has been edited by mr.siro: Feb 11 2019, 07:50 AM
Go to the top of the page
 
cheekybuddha
post Feb 11 2019, 07:50 AM
Post#6


UtterAccess VIP
Posts: 10,880
Joined: 6-December 03
From: Telegraph Hill


OK, but it may be confusing for the user if typing suddenly stops doing anything!!!

Adjust the LimitChars() function - just comment out or remove the MsgBox:
CODE
Function LimitChars(txt As TextBox, MaxChars As Integer) As Boolean

  With txt
    If Len(.Text) > MaxChars Then
'      MsgBox "You have exceeded the maximum length of " & MaxChars & " characters.", vbOKOnly + vbInformation, "To many characters ..."
      .Text = Left(.Text, MaxChars)
      .SelStart = MaxChars
    Else
      LimitChars = True
    End If
  End With

End Function


hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
mr.siro
post Feb 11 2019, 07:57 AM
Post#7



Posts: 147
Joined: 27-January 18



i'm sorry for my bad english.
What i want look like used inputmask property : 00000;0 => user only can type 5 character.
With your code, user still can type 6 character, but after that 6th character will be delete.
This post has been edited by mr.siro: Feb 11 2019, 07:58 AM
Go to the top of the page
 
cheekybuddha
post Feb 11 2019, 08:19 AM
Post#8


UtterAccess VIP
Posts: 10,880
Joined: 6-December 03
From: Telegraph Hill


If you comment out the MsgBox you will never see the 6th character even if the user types it.

--------------------


Regards,

David Marten
Go to the top of the page
 
mr.siro
post Feb 12 2019, 03:58 AM
Post#9



Posts: 147
Joined: 27-January 18



hello cheeky,
I have form with code:
CODE
Private sub GetRec()
some code open connect to SQLserver
Set me.recordset = Recordset From Sqlserver With Parameter
some code close connect


On afterupdate event of txtfilter, i have:
CODE
Call GetRec
me.txtfilter.setfocus


On load event of form:

CODE
Call SetupCharLimits(Me, "txtfilter", 10)


If i type large than 10 character, i get error: you can't reference a property or method for a control unless the control has the focus
At line: .SelStart = MaxChars.
How to fix this.
Go to the top of the page
 
cheekybuddha
post Feb 12 2019, 04:14 AM
Post#10


UtterAccess VIP
Posts: 10,880
Joined: 6-December 03
From: Telegraph Hill


Did you modify SetupCharLimits() function?

The LimitChars() should only fire when the control has focus during it's Change event.

So I don't understand what is causing the focus to be elsewhere when that line of code is called.

Perhaps post a stripped down an with just the form to show the problem.

hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
mr.siro
post Feb 12 2019, 04:33 AM
Post#11



Posts: 147
Joined: 27-January 18



Yes, your is work fine, but in some situation get error, that is when control lost forcus
i think line "Set me.recordset = Recordset From Sqlserver With Parameter" in sub GetRec is reason why your code get error.
I have some test, when set recordset for form, it's will look like requery or refresh, and control lost forcus => get error.
Go to the top of the page
 
cheekybuddha
post Feb 12 2019, 07:01 AM
Post#12


UtterAccess VIP
Posts: 10,880
Joined: 6-December 03
From: Telegraph Hill


Please post SetupCharLimits() and LimitChars() as you have it in your code now

--------------------


Regards,

David Marten
Go to the top of the page
 
mr.siro
post Feb 12 2019, 08:49 PM
Post#13



Posts: 147
Joined: 27-January 18



hello cheeky, you need demo or just code SetupCharLimits() and LimitChars()
With SetupCharLimits() and LimitChars(), i use exactly your code.
This post has been edited by mr.siro: Feb 12 2019, 08:50 PM
Go to the top of the page
 
mr.siro
post Feb 13 2019, 03:22 AM
Post#14



Posts: 147
Joined: 27-January 18



hello cheeky,
i have some with combo box, your code get error: The expression On Change you entered as the event property setting produced the following error: type mismatch.
This post has been edited by mr.siro: Feb 13 2019, 03:23 AM
Go to the top of the page
 
cheekybuddha
post Feb 13 2019, 05:04 AM
Post#15


UtterAccess VIP
Posts: 10,880
Joined: 6-December 03
From: Telegraph Hill


Please post example db which shows this problem

--------------------


Regards,

David Marten
Go to the top of the page
 
mr.siro
post Feb 13 2019, 08:24 PM
Post#16



Posts: 147
Joined: 27-January 18



Restore abc.bak to database name abc. Change connection string in module1.
Form2 get error when type more than 10 character.
This post has been edited by mr.siro: Feb 13 2019, 08:24 PM
Attached File(s)
Attached File  abc.zip ( 144.36K )Number of downloads: 3
 
Go to the top of the page
 
cheekybuddha
post Feb 14 2019, 06:48 AM
Post#17


UtterAccess VIP
Posts: 10,880
Joined: 6-December 03
From: Telegraph Hill


Hi,

I renamed the file to abc.mdb, but it wouldn't open - 'unrecognised database format'

frown.gif

d

--------------------


Regards,

David Marten
Go to the top of the page
 
mr.siro
post Feb 14 2019, 07:41 AM
Post#18



Posts: 147
Joined: 27-January 18



hello cheeky, i open with access2003 or 2010 are good. I don't know why you get error.

This post has been edited by mr.siro: Feb 14 2019, 07:44 AM
Go to the top of the page
 
mr.siro
post Yesterday, 04:33 AM
Post#19



Posts: 147
Joined: 27-January 18



hello cheeky, do you has been open file ?
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    16th February 2019 - 12:32 PM