Wildcard search in VBA?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have checkboxes on a form that I'd like to hide whenever they correspond
to a field ('formname') that contains the word 'Divider'. The word 'Divider'
can appear at any point of the 'formname' field, so I would like to have code
that sets the Visible property of the checkbox to False if the 'formname'
field contains the word 'Divider'. I imiagine this would take place on the
Form's On Load Event, but if there are more efficient ideas out there, I'm
all ears.

Thank you!
 
Hello,

Here is the code I'm trying, but so far without success:

Private Sub Form_Open(Cancel As Integer)
Dim divider As Boolean
If InStr(Me.Formname, "Divider") = True Then
Me.Check24.Visible = False
End If
End Sub

where 'Formname' is a field that, when the word 'Divider' is part of it, I
want to hide the checkbox ('Check24'). These are fields in the 'Detail'
section of my form. When I debug the code, it jumps from the 'If
InStr(Me.Formname, "Divider") = True Then' line to the 'End If' line,
indicating that it does not see 'Formname' as having the word 'Divider' in
it. How can I tweak the above code to hide the desired checkbox when the
field 'Formname' has the word 'Divider' in it?

Thank you.
 
When you say "The word 'Divider' can appear at any point of the 'formname'
field" I assume you mean that the 'formname' field contains a text string and
that 'Divider' can appear at any point within the string. If so, the InStr
function might help you solve your problem. Check your Access help file for
info on how to set it up. There is also info at this link:

http://www.techonthenet.com/access/functions/string/instr.php

HTH
 
That's not how it works. The InStr function returns a numeric value based on
the position of one text string within another. For example, the following is
a text string;

"Find the word Divider in this string"

The InStr function would look for the word Divider in the above string, and
return a numeric value based on it's position within the string. If it did
not find the word it would return 0. So if you were going to use InStr, your
code would need to look more like this;

Private Sub Form_Open(Cancel As Integer)
Dim intDivider As Integer
intDivider = InStr(Me![Formname], "Divider")
If intDivider > 0 Then
Me.Check24.Visible = False
Else
Me.Check24.Visible = True
End If
End Sub

However, I will reiterate that this is only necessary if 'formname' contains
a text string and the word 'Divider' may or may not be inside the string (so
far your posts have been a little unclear on this, so I don't really know).
If 'formname' is just a field that will either have the word 'Divider', or
else some other word, then you don't need to go to all this trouble. Just use
the following type of code;

If Me.formname = "Divider" Then
Me.Check24.Visible = False
Else
Me.Check24.Visible = True
 
Hi Beetle,

The code works great (thank you!). However, the form is set up in
'Continuous Forms' in the Form's Default View, so when it sees the first
record as having the word 'Divider' in the 'formname' field, it blanks out
'Check24' checkbox for ALL records. I tried to see if there was some Event I
could choose on the actual 'Check24' field in the Detail section, but nothing
worked. I can get it to work if I change the Form's Default View to 'Single
Form', but is there a way I can keep it on 'Continuous Forms' and still have
the code act selectively on the records the way I want it to?

Thanks again.
--
Pat Dools


Beetle said:
That's not how it works. The InStr function returns a numeric value based on
the position of one text string within another. For example, the following is
a text string;

"Find the word Divider in this string"

The InStr function would look for the word Divider in the above string, and
return a numeric value based on it's position within the string. If it did
not find the word it would return 0. So if you were going to use InStr, your
code would need to look more like this;

Private Sub Form_Open(Cancel As Integer)
Dim intDivider As Integer
intDivider = InStr(Me![Formname], "Divider")
If intDivider > 0 Then
Me.Check24.Visible = False
Else
Me.Check24.Visible = True
End If
End Sub

However, I will reiterate that this is only necessary if 'formname' contains
a text string and the word 'Divider' may or may not be inside the string (so
far your posts have been a little unclear on this, so I don't really know).
If 'formname' is just a field that will either have the word 'Divider', or
else some other word, then you don't need to go to all this trouble. Just use
the following type of code;

If Me.formname = "Divider" Then
Me.Check24.Visible = False
Else
Me.Check24.Visible = True
Pat Dools said:
Hello,

Here is the code I'm trying, but so far without success:

Private Sub Form_Open(Cancel As Integer)
Dim divider As Boolean
If InStr(Me.Formname, "Divider") = True Then
Me.Check24.Visible = False
End If
End Sub

where 'Formname' is a field that, when the word 'Divider' is part of it, I
want to hide the checkbox ('Check24'). These are fields in the 'Detail'
section of my form. When I debug the code, it jumps from the 'If
InStr(Me.Formname, "Divider") = True Then' line to the 'End If' line,
indicating that it does not see 'Formname' as having the word 'Divider' in
it. How can I tweak the above code to hide the desired checkbox when the
field 'Formname' has the word 'Divider' in it?

Thank you.
 
Back
Top