Unable To Read Value in Masked Textbox

T

techninut

I am trying to build a query using an Access Form. Inside the form are
several controls including some Masked Textboxes for Phone Numbers.

When I try to access the data entered into these Masked Textboxes, I get an
empty string.

Here is what I have so far:

Private Sub cmdSearch_Click()
On Error Resume Next

Dim ctl As Control
Dim sSQL As String
Dim sWhereClause As String

'Initialize the Where Clause variable.
sWhereClause = " Where "

'Start the first part of the select statement.
sSQL = "select * from copy "

'Loop through each control on the form to get its value.
For Each ctl In Me.Controls
With ctl
'The only Control you are using is the text box.
'However, you can add as many types of controls as you want.
Select Case .ControlType
Case acTextBox, acComboBox
.SetFocus
'This is the function that actually builds
'the clause.
If sWhereClause = " Where " Then
sWhereClause = sWhereClause & BuildCriteria(.Name,
dbText, .Text)
Else
sWhereClause = sWhereClause & " and " &
BuildCriteria(.Name, dbText, .Text)
End If
Case acCheckBox
If .Value = True Then
If sWhereClause = " Where " Then
sWhereClause = sWhereClause & "[" & .Name &
"]=True"
Else
sWhereClause = sWhereClause & " and [" & .Name &
"]=True"
End If
End If
End Select
End With
Next ctl

'Set the forms recordsource equal to the new
'select statement.
Me.txtSQL = sSQL & sWhereClause
Me.RecordSource = sSQL & sWhereClause
Me.Requery

End Sub


You can see that I am looking into the values of all TextBoxes, but it
ignores Masked Textboxes.

Any suggestions on what I am doing wrong or something I need to add??

Thank you in advance
 
T

techninut

I have discovered that I need to look at the .Value of the TextBox when
looking at one with a Mask.

I use a check string (checkStr) to see if a value is available. If not, then
I assume that the value was captured in the code above. If a value is found
then I use .Name to specify the field I am looking at and "='" & .Value & "'"
to apply the value. This seems to be working the way I expected it to.

So here is my updated code:

Private Sub cmdSearch_Click()
On Error Resume Next

Dim ctl As Control
Dim sSQL As String
Dim sWhereClause As String
Me.txtSQL = ""

'Initialize the Where Clause variable.
sWhereClause = " Where "

'Start the first part of the select statement.
sSQL = "select * from copy "

'Loop through each control on the form to get its value.
For Each ctl In Me.Controls
With ctl
'The only Control you are using is the text box.
'However, you can add as many types of controls as you want.
Select Case .ControlType
Case acTextBox, acComboBox
.SetFocus
'This is the function that actually builds
'the clause.
If sWhereClause = " Where " Then
sWhereClause = sWhereClause & BuildCriteria(.Name,
dbText, .Text)
Else
sWhereClause = sWhereClause & " and " &
BuildCriteria(.Name, dbText, .Text)
End If
Dim checkStr As String
checkStr = " --: " & .Value & " :-- "
If checkStr = " --: :-- " Then
' Do Nothing
Else
sWhereClause = sWhereClause & " and [" & .Name &
"]='" & .Value & "' "
End If
Case acCheckBox
If .Value = True Then
If sWhereClause = " Where " Then
sWhereClause = sWhereClause & "[" & .Name &
"]=True"
Else
sWhereClause = sWhereClause & " and [" & .Name &
"]=True"
End If
End If
End Select
End With
Next ctl

'Set the forms recordsource equal to the new
'select statement.
Me.txtSQL = sSQL & sWhereClause
Me.RecordSource = sSQL & sWhereClause
Me.Requery

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top