Discussion Locked

Access 2000 SQL Statement

0 helpful
11 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I build a Multi Select List box which puts the following code into a control called Items. [Forms]![Form1]![Items]

Right now Items = Select Name, activity from Table1 where [activity]=MA OR [Activity]=MD OR [Activity]=MS ;

How do I run this SQL code from a button on the Form called Form1????????????

Thanks for your help....
Alan
abramowitza@coned.com

11 replies

by rtviper

Create a function. eg.
Public Function SomeSQLStatement()
DoCmd.RunSQL "SQL Statement goes here"
end function

Next put a button on your form, when the wizard starts to take over, cancel the operation. On the buttons 'OnClick' event, add Call SomeSQLStatement().

The function will now run when the button is clicked

by mra2

Sorry, but it seems that Docmd.RunSQL only works with ACTION queries. I'm using a SELECT query. Any other ideas??

by -keyteck

I believe your programming in VB or VBA. So enumerate all items selected in your list. Call a Function to create a SQLString like the following after selection.

Sub or Function

SQLStr = "Select DISTINCT [Name] From Table1 "

For Each Item In List
If ItemX = Selected Then SQLStr = SQLStr & "Where ItemX ='Something'"
If Previous Item = Selected then SQLStr = SQLStr & " OR "
Next Item In List

End Sub or Function

After the SQLStr is built the call the string like RecordSet = Database.OpenRecordset(SQLStr).

Let me know it this works or doesn't at keyteck-at-comcast.net-NoMassEmails, please I'm curious. -Walter

by d_gilmour_1978

You need to assign the results of the DoCmd.runSQL("Select * from bob") to a variable for you to have any chance of doing any thing with it.
I am however unsure if it does work.

you could also retrieve an ado(or dao) recordset and loop through it adding the items to the listbox

Function GetData(ByVal strSQL As String, strCon As String) As ADODB.Recordset
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command

With cnn
.CommandTimeout = 2000
.Open strCon
End With
With cmd
.ActiveConnection = cnn
.CommandType = adCmdText
.CommandText = strSQL
End With
Set rs = cmd.Execute
Set GetData = rs
End Function

This is a generic function I use in access to access external data however you can modifiy it to work with access tables by using CurrentProject.ActiveConnection instead of the cnn for the cmd object.

This discussion is closed to new replies.