Using wild cards in VBA

G

Guest

How can I re-write the below code to allow users to use wildcards in the
SearchPart field. They'd like to be able to type S* and return all
SearchParts that start with S or type St* and return all SearchParts that
start with St. I'm not sure how to write it. Thanks.

stLinkCriteria = "[Part#]=" & "'" & Me![SearchPart] & "'"
If IsNull(Me.Model) = False Then
stLinkCriteria = stLinkCriteria & "AND [Model#]=" & "'" & Me![Model] & "'"
End If
DoCmd.OpenForm stDocName, , , stLinkCriteria

If IsNull(Me.Model) Then
Forms!NewPartInputfrm!CountRecords = DCount("[Part#]", "AllNewParts",
"[PART#] = [SearchPart]")

Else
 
V

Van T. Dinh

Try:

Forms!NewPartInputfrm!CountRecords = DCount("[Part#]", "AllNewParts", _
"[PART#] Like '" & [SearchPart] "*'")

(users only need to enter S or St)
 
G

Guest

Why the DCount, Van?
The OP wants the records, not the count.

Van T. Dinh said:
Try:

Forms!NewPartInputfrm!CountRecords = DCount("[Part#]", "AllNewParts", _
"[PART#] Like '" & [SearchPart] "*'")

(users only need to enter S or St)

--
HTH
Van T. Dinh
MVP (Access)



Alex said:
How can I re-write the below code to allow users to use wildcards in the
SearchPart field. They'd like to be able to type S* and return all
SearchParts that start with S or type St* and return all SearchParts that
start with St. I'm not sure how to write it. Thanks.

stLinkCriteria = "[Part#]=" & "'" & Me![SearchPart] & "'"
If IsNull(Me.Model) = False Then
stLinkCriteria = stLinkCriteria & "AND [Model#]=" & "'" & Me![Model] &
"'"
End If
DoCmd.OpenForm stDocName, , , stLinkCriteria

If IsNull(Me.Model) Then
Forms!NewPartInputfrm!CountRecords = DCount("[Part#]", "AllNewParts",
"[PART#] = [SearchPart]")

Else
 
G

Guest

Perfect! Thank you.

Douglas J. Steele said:
stLinkCriteria = "[Part#] Like " & "'" & Me![SearchPart] & "*'"

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Alex said:
How can I re-write the below code to allow users to use wildcards in the
SearchPart field. They'd like to be able to type S* and return all
SearchParts that start with S or type St* and return all SearchParts that
start with St. I'm not sure how to write it. Thanks.

stLinkCriteria = "[Part#]=" & "'" & Me![SearchPart] & "'"
If IsNull(Me.Model) = False Then
stLinkCriteria = stLinkCriteria & "AND [Model#]=" & "'" & Me![Model] &
"'"
End If
DoCmd.OpenForm stDocName, , , stLinkCriteria

If IsNull(Me.Model) Then
Forms!NewPartInputfrm!CountRecords = DCount("[Part#]", "AllNewParts",
"[PART#] = [SearchPart]")

Else
 
D

Douglas J. Steele

The OP had the DCount in his question.

You & I concentrated on the first part of the VBA, Van apparently
concentrated on the second part. <g>

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Klatuu said:
Why the DCount, Van?
The OP wants the records, not the count.

Van T. Dinh said:
Try:

Forms!NewPartInputfrm!CountRecords = DCount("[Part#]", "AllNewParts",
_
"[PART#] Like '" & [SearchPart] "*'")

(users only need to enter S or St)

--
HTH
Van T. Dinh
MVP (Access)



Alex said:
How can I re-write the below code to allow users to use wildcards in
the
SearchPart field. They'd like to be able to type S* and return all
SearchParts that start with S or type St* and return all SearchParts
that
start with St. I'm not sure how to write it. Thanks.

stLinkCriteria = "[Part#]=" & "'" & Me![SearchPart] & "'"
If IsNull(Me.Model) = False Then
stLinkCriteria = stLinkCriteria & "AND [Model#]=" & "'" & Me![Model]
&
"'"
End If
DoCmd.OpenForm stDocName, , , stLinkCriteria

If IsNull(Me.Model) Then
Forms!NewPartInputfrm!CountRecords = DCount("[Part#]",
"AllNewParts",
"[PART#] = [SearchPart]")

Else
 
V

Van T. Dinh

I claimed I have just got up and the logical part of the brain is not quite
there yet when I posted the reply ...
 

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