Multiple WHERE clauses in docmd.openform (1 Viewer)

Steve@trop

Registered User.
Local time
Today, 02:44
Joined
May 10, 2013
Messages
148
I'm trying to open a form to a specific record using the criteria in the docmd.openform command. I know the criteria are correct individually because they both work when I use them one at a time but when I try to put them together I get errors. Here is how I have it together in it's latest form:

Code:
     DoCmd.OpenForm "CTableSearchF", , , "[ctable].[lastname] = '" & LastNamestr & "'" And "[ctable].[phonenumber] = '" & PhoneNumberstr & "'"
When I run it I get a "type mismatch" error. If I pull out one or the other criteria (before or after the "and") it works fine but I need both criteria to open the correct record.

I know it's probably something really simple like a different type of quote or something. I know it's really close because the criteria work OK by themselves it's just when I try to put them together that it fails.

As usual any type of help you can give on this is greatly appreciated!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:44
Joined
Aug 30, 2003
Messages
36,126
You've made a common mistake. Delete the double quotes on each side of And.
 

ozinm

Human Coffee Siphon
Local time
Today, 10:44
Joined
Jul 10, 2003
Messages
121
I think you've got a " in the wrong place.

try this:
Code:
 DoCmd.OpenForm "CTableSearchF", , , "[ctable].[lastname] = '" & LastNamestr & "'And [ctable].[phonenumber] = '" & PhoneNumberstr & "'"
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:44
Joined
Aug 30, 2003
Messages
36,126
I think you've got a " in the wrong place.

try this:
Code:
 DoCmd.OpenForm "CTableSearchF", , , "[ctable].[lastname] = '" & LastNamestr & "'" "And [ctable].[phonenumber] = '" & PhoneNumberstr & "'"

That will fail as well. It could work if you threw an & in there, but the extra concatenation isn't necessary.
 

ozinm

Human Coffee Siphon
Local time
Today, 10:44
Joined
Jul 10, 2003
Messages
121
That will fail as well. It could work if you threw an & in there, but the extra concatenation isn't necessary.

Yep sorry. Half asleep!.

This should do:

Code:
DoCmd.OpenForm "CTableSearchF", , , "[ctable].[lastname] = '" & LastNamestr & "' And [ctable].[phonenumber] = '" & PhoneNumberstr & "'"

Also it might be worth chucking in a couple of nz's in case you have null values in the vars.
 

Steve@trop

Registered User.
Local time
Today, 02:44
Joined
May 10, 2013
Messages
148
Thanks all for your help. I tried the last one first and it worked:).

About the "nz": I'm curious (how would that work?) but pretty sure I won't need it in this case. This code is part of a script that opens an existing record if the user attempts to create a new record that duplicates one that already exists in the table (violating a table index). The violation won't occur unless both fields have a value that matches an existing record so neither should be null.

Thanks again!

Steve
 

ozinm

Human Coffee Siphon
Local time
Today, 10:44
Joined
Jul 10, 2003
Messages
121
Thanks all for your help. I tried the last one first and it worked:).

About the "nz": I'm curious (how would that work?) but pretty sure I won't need it in this case. This code is part of a script that opens an existing record if the user attempts to create a new record that duplicates one that already exists in the table (violating a table index). The violation won't occur unless both fields have a value that matches an existing record so neither should be null.

Thanks again!

Steve

so Nz can be used to return a value if a variable (or function / calculation) returns a null.
So you can do something like this:
Code:
DoCmd.OpenForm "CTableSearchF", , , "[ctable].[lastname] = '" & Nz(LastNamestr,"Name Unknown") & "'" And "[ctable].[phonenumber] = '" & Nz(PhoneNumberstr,"Phone Unknown") & "'"

Also when concatenating strings together it's sometimes useful to understand the difference between the + and & operators.

The + will join strings together but if one of the variables is null the entire result will be null.
The & will join strings together and ignore any nulls.

This can be useful as a quick way to avoid adding extra spaces, commas etc.

eg
Code:
FirstName = "John"
MiddleNames = NULL
LastName = "Smith"

'This will display "John◊Smith" with only one space
debug.print FirstName & Nz("◊" + MiddleNames , "") & "◊" & LastName
'This is because " " + Middlenames = NULL

'This will display "John◊◊Smith" with two spaces
debug.print FirstName & "◊" & MiddleNames &  "◊" & LastName
NB: I've replaced the space characters with ◊'s to make it easier to see.
 

Falcone203

Member
Local time
Today, 04:44
Joined
Dec 5, 2019
Messages
40
Dim st1 As Integer
Dim st2 As Integer

If (IsNull(Me![Sub Item])) = False Then
st1 = Me![Sub ID]
st2 = Me![IID]

If (IsNull([st1])) Then
'Do Nothing
Else
DoCmd.OpenForm "43 FIRE DEPT subform2", acNormal, , "[43 fire dept].id = " & st1 & "And [tblfae].FAIDFK = " & st2

End If
End If
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:44
Joined
Aug 30, 2003
Messages
36,126
Dim st1 As Integer
Dim st2 As Integer

If (IsNull(Me![Sub Item])) = False Then
st1 = Me![Sub ID]
st2 = Me![IID]

If (IsNull([st1])) Then
'Do Nothing
Else
DoCmd.OpenForm "43 FIRE DEPT subform2", acNormal, , "[43 fire dept].id = " & st1 & "And [tblfae].FAIDFK = " & st2

End If
End If

Are you asking a question or is this an answer to a 5 year old question?
 

Users who are viewing this thread

Top Bottom