PC Review Forums Software Windows XP & Applications MS Access SQL query using a value from a combobox

Reply
 
Thread Tools Rate Thread
Old 21-04-2008, 03:37 PM   #1
arresth
Junior Member
 
Join Date: Apr 2008
Posts: 2
Trader Rating: (0)
Default MS Access SQL query using a value from a combobox

Hi guys, I have a question that has been trying to kill me for the whole day. I'm pretty sure it's not going to be hard for you, if it is possible.
I am trying to run a SELECT query on a table, but I want the WHERE to be defined by a selection in a combo box. So the value for WHERE should be selected by the user. Is that possible?

I will show you what I've done so far.

Private Sub cboSector_Change()
Dim SQL As String
Dim Sector As String

SQL = "SELECT FirstName, Surname, Title FROM AttendanceArchive WHERE SchoolService = cboSector.Text"

lstOutcome.RowSource = SQL
End Sub

No need to tell you I've tried all variations of the more obvious stuff. Changing, rearranging.. didn't help me. I have the feeling I'm missing something very basic.. or it might not be possible.

I chose this path of doing things, because in my old database (Attendance is now being moved to the new one, thus becoming Archive) I have a similar thing, but input comes from an inputbox. The following statement works perfectly fine in my old database.

Private Sub cmdFind_Click()
Dim sql As String
sql = "SELECT FirstName, Surname, Title FROM Attendance WHERE FirstName = EmployeeName OR Surname = EmployeeName"
lstOutcome.RowSource = sql
End Sub

It's been a while since I used VB, so a bit of a helping hand will be grately appreciated.
Thanks in advance

Last edited by arresth : 21-04-2008 at 03:41 PM.
arresth is offline   Reply With Quote
Old 25-04-2008, 11:57 AM   #2
Rockin_R63
Junior Member
 
Rockin_R63's Avatar
 
Join Date: Apr 2008
Posts: 3
Trader Rating: (0)
Default

arresth

I am no expert, but there are 2 things you can try:

First:
SQL = "SELECT FirstName, Surname, Title FROM AttendanceArchive WHERE SchoolService = cboSector.Text"
'.text' is used to write to a field, whereas '.value' is used to read from a field. Try changing cboSector.Text to cboSector.Value?

Second:
What you are trying to do is called a 'parameter query'. I have never done this directly thru code, but it is a doddle if you design your query in the query designer. Have a look at Access Help, its quite useful on the topic.

Best regards
Ron
Rockin_R63 is offline   Reply With Quote
Old 25-04-2008, 02:32 PM   #3
arresth
Junior Member
 
Join Date: Apr 2008
Posts: 2
Trader Rating: (0)
Default

Thanks Ron, .Value was what i was looking for. Good man
arresth is offline   Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off