Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating SQL string - Please help!!

Status
Not open for further replies.

hirenJ

Programmer
Dec 17, 2001
72
0
0
GB

Hi all, can anyone shed ANY light on this SQL query?!! Ive been puzzled over this for month -- ive tried generating the Query in 10 different ways, including different syntax, using the ADO.parameter object, command object --- still not working!!

At the moment I have been passing strRespCode to the function. This is of text data type and represents a numeric code i.e. 002 -- the SQL string generates correctly, however, it doesnt seem to execute -- any ideas?

Hj

===========================================================

Public Function Get_Contacts2(strRespCode As String)
'GET number of contacts withselected respCode

Dim rst As New ADODB.Recordset
Dim cmd As ADODB.Command
Dim strSQL As String
Dim quo As String
Dim criteria As String
'database connection
Set conn = CurrentProject.Connection
Set rst = New ADODB.Recordset
'generate SQL string
quo = Chr$(34)

criteria = "*" & strRespCode & "*"

strSQL = "SELECT Contact_Table.RESPCODE
FROM Contact_Table GROUP BY Contact_Table.RESPCODE
HAVING (((Contact_Table.RESPCODE) Like'" & criteria & "'));"

Debug.Print strSQL

rst.Open strSQL, conn, adOpenStatic, adLockReadOnly, adCmdText

'get recordcount
If (rst.EOF = True And rst.BOF = True) Then
MsgBox ("Houston we have a problem")
Else

Do Until rst.EOF
Debug.Print rst![RespCode]
rst.MoveNext
Loop
End If
Stop

Set rst = Nothing
Set conn = Nothing
Set cmd = Nothing
End Function
===========================================================
 
I found one problem in your sql statement. You should use WHERE instead of HAVING.

strSQL = "SELECT Contact_Table.RESPCODE
FROM Contact_Table
WHERE (((Contact_Table.RESPCODE) Like '" & criteria & "'
GROUP BY Contact_Table.RESPCODE
));"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top