Syntax question about stLinkCriteria

B

BrettS

I am currently using the following code for a button to go from one form to
the other, while remaining on the same Job #:

Private Sub OpenFile_Click()
On Error GoTo Err_OpenFile_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "FileData"
stLinkCriteria = "[RE Job #]=" & "'" & Me![RE Job #] & "'"

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_OpenFile_Click:
Exit Sub

Err_OpenFile_Click:
MsgBox Err.Description
Resume Exit_OpenFile_Click
End Sub

I would like to do the same thing, only with not only with[RE Job #],
[Sample #] matching Me![Sample #], but I can't seem to figure out the syntax
to get it to work correctly. Something like:

Private Sub OpenFile_Click()
On Error GoTo Err_OpenFile_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "FileData"
stLinkCriteria = "[RE Job #]=" & "'" & Me![RE Job #] & "'" and "[Sample
#]=" & "'" & Me![Sample #] & "'"

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_OpenFile_Click:
Exit Sub

Err_OpenFile_Click:
MsgBox Err.Description
Resume Exit_OpenFile_Click
End Sub



However, when I try this I get a "Type mismatch" error message. Anything
thanks would be appreciated.
 
D

Douglas J. Steele

It's supposed to be a valid Where clause, without the word Where in front.

stLinkCriteria = "[RE Job #]='" & Me![RE Job #] & _
"' and [Sample #]='" & Me![Sample #] & "'""

Exagerated for clarity, that's


stLinkCriteria = "[RE Job #]= ' " & Me![RE Job #] & _
" ' and [Sample #]= ' " & Me![Sample #] & " ' ""
 
R

RoyVidar

BrettS said:
I am currently using the following code for a button to go from one
form to the other, while remaining on the same Job #:

Private Sub OpenFile_Click()
On Error GoTo Err_OpenFile_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "FileData"
stLinkCriteria = "[RE Job #]=" & "'" & Me![RE Job #] & "'"

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_OpenFile_Click:
Exit Sub

Err_OpenFile_Click:
MsgBox Err.Description
Resume Exit_OpenFile_Click
End Sub

I would like to do the same thing, only with not only with[RE Job #],
[Sample #] matching Me![Sample #], but I can't seem to figure out the
syntax to get it to work correctly. Something like:

Private Sub OpenFile_Click()
On Error GoTo Err_OpenFile_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "FileData"
stLinkCriteria = "[RE Job #]=" & "'" & Me![RE Job #] & "'" and
"[Sample #]=" & "'" & Me![Sample #] & "'"

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_OpenFile_Click:
Exit Sub

Err_OpenFile_Click:
MsgBox Err.Description
Resume Exit_OpenFile_Click
End Sub



However, when I try this I get a "Type mismatch" error message.
Anything thanks would be appreciated.

Concatenation of strings, try

stLinkCriteria = "[RE Job #]='" & Me![RE Job #] & "' and [Sample
#]='" & Me![Sample #] & "'"

Anyway, when doing string concatenation, you should be able to do a

Debug.Print stLinkCriteria

in the code/immediate pane, and see the results in the immediate pane
(ctrl+g), which should become a valid SQL WHERE clause, without the
keyword "Where"

[RE Job #]='42' and [Sample#]='42'"

Single quotes are necessary only for criteria on fields defined as
text.
 
B

BrettS

I would like to do the same thing, only with not only with[RE Job #],
[Sample #] matching Me![Sample #], but I can't seem to figure out the syntax
to get it to work correctly. Something like:


Sorry but I completely butchered the first sentence of this part when I
first wrote this up. I meant to say, "I would like to do the same thing, but
not only with [RE Job #], but also [Sample #] matching Me![Sample #]..."
 
B

BrettS

Thanks for your responses. Both answers were slightly different. Douglas'
still gave me a syntax error, while Roy's just brings up a "OpenForm command
was canceled" message box.
 
R

RoyVidar

BrettS said:
Thanks for your responses. Both answers were slightly different.
Douglas' still gave me a syntax error, while Roy's just brings up a
"OpenForm command was canceled" message box.

Drop off the last quote in Douglas Steel's suggestion, and see if that
works, if not, please post what are the datatypes of your fields.

In case the last one is numeric, you need to strip off the single
quotes

stLinkCriteria = "[RE Job #]='" & Me![RE Job #] & _
" and [Sample#]='" & Me![Sample #]


Perhaps also, post the result of Debug.Print stLinkCriteria

Debug.Print stLinkCriteria
 
R

RoyVidar

RoyVidar said:
In case the last one is numeric, you need to strip off the single
quotes

stLinkCriteria = "[RE Job #]='" & Me![RE Job #] & _
" and [Sample#]='" & Me![Sample #]

Oups - typo

In case the last one is numeric, you need to strip off the single
quotes

stLinkCriteria = "[RE Job #]='" & Me![RE Job #] & _
" and [Sample#]=" & Me![Sample #]
 
B

BrettS

RoyVidar said:
Oups - typo

In case the last one is numeric, you need to strip off the single
quotes

stLinkCriteria = "[RE Job #]='" & Me![RE Job #] & _
" and [Sample#]=" & Me![Sample #]



Yes - you are correct, the sample # field is numeric. When I used that
code, I get the error message:
Syntax error in string in query expression '[RE Job #]='A1226 and [Sample
#]=2'.

(With A1226 and 2 being the values in the appropriate fields.)
 
R

RoyVidar

BrettS said:
RoyVidar said:
Oups - typo

In case the last one is numeric, you need to strip off the single
quotes

stLinkCriteria = "[RE Job #]='" & Me![RE Job #] & _
" and [Sample#]=" & Me![Sample #]



Yes - you are correct, the sample # field is numeric. When I used
that code, I get the error message:
Syntax error in string in query expression '[RE Job #]='A1226 and
[Sample #]=2'.

(With A1226 and 2 being the values in the appropriate fields.)

'nother typo

stLinkCriteria = "[RE Job #]='" & Me![RE Job #] & _
"' and [Sample#]=" & Me![Sample #]
 

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