SQL query to subform (1 Viewer)

calvinle

Registered User.
Local time
Yesterday, 20:12
Joined
Sep 26, 2014
Messages
332
Hi,

I don't know where is the issue but I am having issue to display the result in my subform using the following query:

Can anyone help on it?

Code:
  Dim strWhere As String              
  Dim rsResult As Recordset
  
  strWhere = "SELECT * FROM tblWork WHERE client_id = " & Nz(Me.cboClient.Value)
  
  Set rsResult = CurrentDb.OpenRecordset(strWhere, dbOpenSnapshot)
  
  Me.frmWork.SourceObject = "sfWork"
  Set Me.frmWork.Form.Recordset = rsResult

The SQL string shows:
Code:
SELECT * FROM tblWork WHERE client_id = "1A1"

but the subform does not display any data.

Thanks
 

bob fitz

AWF VIP
Local time
Today, 04:12
Joined
May 23, 2011
Messages
4,727
Perhaps:

Code:
Me.frmWork.Form.Recordset = "SELECT * FROM tblWork WHERE client_id = " & Nz(Me.cboClient.Value)
 

isladogs

MVP / VIP
Local time
Today, 04:12
Joined
Jan 14, 2017
Messages
18,246
Client_id is a string so you need text delimiters

Code:
  "SELECT * FROM tblWork WHERE client_id = '" & Nz(Me.cboClient.Value) & "'"
 

calvinle

Registered User.
Local time
Yesterday, 20:12
Joined
Sep 26, 2014
Messages
332
Client_id is a string so you need text delimiters

Code:
  "SELECT * FROM tblWork WHERE client_id = '" & Nz(Me.cboClient.Value) & "'"

Finally it works!!

But I though both can work because it's string..? Either " or '
 

isladogs

MVP / VIP
Local time
Today, 04:12
Joined
Jan 14, 2017
Messages
18,246
What you had would have worked if the field was a number but not for text
 

calvinle

Registered User.
Local time
Yesterday, 20:12
Joined
Sep 26, 2014
Messages
332
This is my main code that I use during that filtering:

Code:
strWhere = strWhere & "([client_id] Like ""*" & Me.cboClientId & "*"") AND "

How should I change this one so it works?

Thanks
 

Users who are viewing this thread

Top Bottom