473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Help Setting Static Combobox Values to SQL SELECT Query

Hi, two parts to my problem if someone can help address either one or
both:

1. I want to SELECT everything in the table if it matches the
criteria when the query button is pressed (this is just a
commandbutton on my form). The biggest problem I'm having is that I
can't (don't know how) populate a combobox on static items such as
"True, False, Any." The Any field, would be selected by the user to
find all records where that fields is either True or False. So how
can I set the values in the combo box to those three options? Perhaps
when I start the form (it shows up) that I should use the
combobox1.AddItem "True", etc.? Then where do I write the code for
when the form initiates? Right now, I just have it set up that I go
to Forms in the Access database and double click the corresponding
Form to initiate.

2. The second problem lies in being able to write the code that would
get the text selected from the combobox (I don't want the user to just
type anything; instead, need the user to just select the proper value)
and then based on that, run the SQL query satisfying those
requirements. I understand True = -1, False = 0. It's the pain of
having to have the Any to search for both True and False records for a
particular field that adds so much trouble. So again, combobox with
values "True, False, Any" and being able to implement those
corresponding values into a SQL SELECT statement. The DoCmd.RunSQL in
VB, unfortunately, does not work with SELECT statements (only work
with action queries).

I am wondering if "recordset" will work in VB? I've never used it and
am not sure how it works. How would I "mount" the results of the
recordset into the dataview or similar table-like format?

Thanks a lot (this has hammering me for the last three days!!!)

Daryl

Jul 18 '07 #1
1 8022
For 1): Static is easy: set the .ComboBox .RowSourceType to
'ValueList' and the.RowSource to your list of selection values
separated by semicolons (or commas): "True";"False";"Any"

Note that choosing the 'Any' Option, is logically the same as having
NO selection at all...so just skip the selection...

For 2) The easiest way to do what you are trying to do is to build a
SQL 'WHERE' clause as a text string (without the word WHERE) and then
set the .Form's .Filter property to that SQL clause (and also
set .FilterOn true)

The sample code here is clipped from an application that selectively
filters a Form displaying set of course offerings based on date and
time offered and the Subject=>Course=>Section the user want to see.
Each time any of the selection combo boxes is changed, the filter is
rebuilt. In this sample, I use the text string "{ALL}" to indicate any
value is accepted, note that if the ALL value is selected, the filter
builder simply skips building that section of the select. The curly
braces sort the ALL value to the top of the selection list (first item
in the combo box...)

Private Sub usSetDate_OnChange()
SetFilter
End Sub

Private Sub usSetSubject_OnChange()
SetFilter
End Sub

Private Sub usSetCourse_OnChange()
SetFilter
End Sub

Private Sub usSetSection_OnChange()
SetFilter
End Sub

etc...for as many combo box selectors you want...
....
<compiled code>

Public Sub SetFilter()
Dim wkFilter As String
Dim wkDate As String
Dim wkTime As String
Dim wkSubject As String
Dim wkCourse As String
Dim wkSection As String

On Error GoTo ProcErr

Me.Filter = ""
Me.FilterOn = False
Me.Painting = False

If (Len(Me.usSetDate) 0) Then
wkDate = CDate(DatePart("m", Me.usSetDate) & "/" & DatePart("d",
Me.usSetDate) & "/" & DatePart("yyyy", Me.usSetDate))
wkTime = CDate(DatePart("h", Me.usSetDate) & ":" & DatePart("n",
Me.usSetDate) & ":" & DatePart("s", Me.usSetDate) & IIf(Right$
(Me.usSetDate, 1) = "M", Right$(Me.usSetDate, 2), ""))

Select Case (wkTime)
Case "12:00:00 AM"
wkFilter = "([EventDate] = #" & wkDate & "#) AND "
Case Else
wkFilter = "([EventDate] = #" & wkDate & "#) AND " & _
"([EventStartTime] <= #" & wkTime & "#) AND " & _
"([EventEndTime] >= #" & wkTime & "#) AND "
End Select
End If

wkSubject = Me.usSetSubject
wkCourse = Me.usSetCourse
wkSection = Me.usSetSection

If (wkSubject <"{ALL}") Then
If (Len(wkSubject) 0) Then
wkFilter = wkFilter & "([Subject] = '" & wkSubject & "') AND "
Else
wkFilter = wkFilter & "(IsNull([Subject]) = True) AND "
End If
End If

If (wkCourse <"{ALL}") Then
If (Len(wkCourse) 0) Then
wkFilter = wkFilter & "([Course] = '" & wkCourse & "') AND "
Else
wkFilter = wkFilter & "(IsNull([Course]) = True) AND "
End If
End If

If (wkSection <"{ALL}") Then
If (Len(wkSection) 0) Then
wkFilter = wkFilter & "([Section] = '" & wkSection & "') AND "
Else
wkFilter = wkFilter & "(IsNull([Section]) = True) AND "
End If
End If

If (Len(wkFilter) 0) Then
Me.Filter = Mid$(wkFilter, 1, Len(wkFilter) - 5)
Me.FilterOn = True
End If

ProcExit:
Me.Painting = True
Me.Repaint
Exit Sub

ProcErr:
Select Case Err
Case 0:
Resume Next
Case Else
' Display 0, "SetFilter Error: " & Err & " " & Err.Description &
" " & Now()
LogErr "SetFilter", Err, Err.Description
Resume ProcExit
End Select
End Sub

</end compiled code>

Good luck with sorting this out...

Ron, King of Chi

On Jul 18, 10:31 am, "The.Daryl...@gmail.com" <The.Daryl...@gmail.com>
wrote:
Hi, two parts to my problem if someone can help address either one or
both:

1. I want to SELECT everything in the table if it matches the
criteria when the query button is pressed (this is just a
commandbutton on my form). The biggest problem I'm having is that I
can't (don't know how) populate a combobox on static items such as
"True, False, Any." The Any field, would be selected by the user to
find all records where that fields is either True or False. So how
can I set the values in the combo box to those three options? Perhaps
when I start the form (it shows up) that I should use the
combobox1.AddItem "True", etc.? Then where do I write the code for
when the form initiates? Right now, I just have it set up that I go
to Forms in the Access database and double click the corresponding
Form to initiate.

2. The second problem lies in being able to write the code that would
get the text selected from the combobox (I don't want the user to just
type anything; instead, need the user to just select the proper value)
and then based on that, run the SQL query satisfying those
requirements. I understand True = -1, False = 0. It's the pain of
having to have the Any to search for both True and False records for a
particular field that adds so much trouble. So again, combobox with
values "True, False, Any" and being able to implement those
corresponding values into a SQL SELECT statement. The DoCmd.RunSQL in
VB, unfortunately, does not work with SELECT statements (only work
with action queries).

I am wondering if "recordset" will work in VB? I've never used it and
am not sure how it works. How would I "mount" the results of the
recordset into the dataview or similar table-like format?

Thanks a lot (this has hammering me for the last three days!!!)

Daryl

Jul 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
20
by: Jack Schitt | last post by:
I thought I was starting to get a handle on Access, until I tried doing something useful...now I'm stuck. I have a DB with two tables - to keep it simple I'll say that one is an Employee File...
5
by: -elniniol999- | last post by:
I am DESPERATE for a quick reply.. have exhausted my options... i have a table called order details which contains: order id product id product name quantity unit price sale price
6
by: sheree | last post by:
I would like to create a query where one of the columns of the queries comes from a combo list box on a form. For example, if my table has the following fields: id name interest1 interest2...
1
by: jvb | last post by:
Hello, I have got the following problem. I have an access database with the name of different persons and there telephone numbers. In an access page you can display the field name and the...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
9
by: Edwinah63 | last post by:
Hi everyone, Please let there be someone out there who can help. I have two BOUND combo boxes on a continuous form, the second being dependent on the first. I have no problem getting the...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
1
by: Thelma Roslyn Lubkin | last post by:
I have a form with several comboboxes whose contents I'm trying to initialize from a table. The table, ComboSelections, has 3 fields, an autonumber ID and two text fields, ListCode and ListEntry....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.