473,490 Members | 2,695 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ComboBox using SQL code

Stu
Hi,
I've been working on trying to use a combo box to filter my records for
a while now, and can't get it to work. Right now, I have SQL code
written into IfThen statements on the afterupdate for the combobox. I
tried using the docmd.runSQL but from looking at previous posts, it
seems that I need to use DoCmd.OpenQuery instead since I do not have an
action SQL statement.
The short term problem is that when the user selects from the combobox,
Im getting a "type mis-match error." The longer term problem is that
once the records are filtered, I need to use the procedure number of
the filtered record to filter another set of documents(will worry about
this problem later). It might just something simple with my code, but
I'm not sure. Any help would be VERY much appreciated.

Some details/questions:
-combobox (combo1): unbound
-Does the filtered records need to be in a subform, or can it be in the
same form as the combobox?
-the combo box has two fields, only showing the second one that has the
text in it. The first hidden column is a [TypeID] that I was trying to
use in the code.

code:
************************
Private Sub combo1_AfterUpdate()
Dim ComboSortSQL As String
Dim dbs As Database
Dim qdf As QueryDefs

Set dbs = CurrentDb()
If combo1.Value = "1" Then 'Standard Procedures
ComboSortSQL = "SELECT * FROM tbl_Procedures WHERE
tbl_Procedures.[TypeID] = 1;"
Set qdf = dbs.CreateQueryDef("ComboSortSQL", ComboSortSQL)
DoCmd.OpenQuery qdf
DoCmd.DeleteObject acQuery, ComboSortSQL
ElseIf combo1.Value = 2 Then 'Policy
ComboSortSQL = "SELECT * FROM tbl_Procedures WHERE
tbl_Procedures.TypeID.Value = 2;"
Set qdf = dbs.CreateQueryDef("ComboSortSQL", ComboSortSQL)
DoCmd.OpenQuery qdf
DoCmd.DeleteObject acQuery, ComboSortSQL
ElseIf combo1.Value = 3 Then 'Process Description
ComboSortSQL = "SELECT * FROM tbl_Procedures WHERE
tbl_Procedures.TypeID.Value = 3;"
Set qdf = dbs.CreateQueryDef("ComboSortSQL", ComboSortSQL)
DoCmd.OpenQuery qdf
DoCmd.DeleteObject acQuery, ComboSortSQL
ElseIf combo1.Value = 4 Then 'Program Description
ComboSortSQL = "SELECT * FROM tbl_Procedures WHERE
tbl_Procedures.TypeID.Value = 4;"
Set qdf = dbs.CreateQueryDef("ComboSortSQL", ComboSortSQL)
DoCmd.OpenQuery qdf
DoCmd.DeleteObject acQuery, ComboSortSQL
ElseIf combo1.Value = 5 Then 'Training Program
ComboSortSQL = "SELECT * FROM tbl_Procedures WHERE
tbl_Procedures.TypeID.Value = 5;"
Set qdf = dbs.CreateQueryDef("ComboSortSQL", ComboSortSQL)
DoCmd.OpenQuery qdf
DoCmd.DeleteObject acQuery, ComboSortSQL
ElseIf combo1.Value = 6 Then 'Qualification
ComboSortSQL = "SELECT * FROM tbl_Procedures WHERE
tbl_Procedures.TypeID.Value = 6;"
Set qdf = dbs.CreateQueryDef("ComboSortSQL", ComboSortSQL)
DoCmd.OpenQuery qdf
DoCmd.DeleteObject acQuery, ComboSortSQL
ElseIf combo1.Value = 7 Then 'Standard / Specification
ComboSortSQL = "SELECT * FROM tbl_Procedures WHERE
tbl_Procedures.TypeID.Value = 7;"
Set qdf = dbs.CreateQueryDef("ComboSortSQL", ComboSortSQL)
DoCmd.OpenQuery qdf
DoCmd.DeleteObject acQuery, ComboSortSQL
ElseIf combo1.Value = 8 Then 'Guidance & Reference Documents
ComboSortSQL = "SELECT * FROM tbl_Procedures WHERE
tbl_Procedures.TypeID.Value = 8;"
Set qdf = dbs.CreateQueryDef("ComboSortSQL", ComboSortSQL)
DoCmd.OpenQuery qdf
DoCmd.DeleteObject acQuery, ComboSortSQL
End If

dbs.Close
Set qdf = Nothing
Set dbs = Nothing

End Sub
**********************

Nov 13 '05 #1
2 3149
What are you trying to do? If it's just display then filter records
from a table, add the appropaiate controls to your form and then add a
combo for filtering the TypeID. In the AfterUpdate event code use:

If IsNull(Me.[cboFilterType])=True Then Exit Sub
' otherwise ...
Dim i As Integer
Dim strFilter As String
i = Me.[cboFilterType]
strFilter = "[TypeID]=" & i
DoCmd.ApplyFilter , strFilter

If you want to count the number of records meeting the filter criteria,
include a DCount in your code:

Dim intRecs As Integer
(or use a Long if you have more than 32,000 recs)
intRecs = DCount("[TypeID]", "tbl_Procedures", "[TypeID]=" & i)

If that isn't what you need and you can still eliminate all the
redundant code in your If...Then...ElseIf:

Dim i As Integer
i = Me.[combo1]
ComboSortSQL = "SELECT * FROM tbl_Procedures WHERE
tbl_Procedures.TypeID=" & i

Hope this helps!

Howard Brody
> >


Nov 13 '05 #2
"Stu" <ka*******@vcu.edu> wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
Hi,
I've been working on trying to use a combo box to filter my
records for a while now, and can't get it to work. Right now,
I have SQL code written into IfThen statements on the
afterupdate for the combobox. I tried using the docmd.runSQL
but from looking at previous posts, it seems that I need to
use DoCmd.OpenQuery instead since I do not have an action SQL
statement. The short term problem is that when the user
selects from the combobox, Im getting a "type mis-match
error." The longer term problem is that once the records are
filtered, I need to use the procedure number of the filtered
record to filter another set of documents(will worry about
this problem later). It might just something simple with my
code, but I'm not sure. Any help would be VERY much
appreciated.

Some details/questions:
-combobox (combo1): unbound
-Does the filtered records need to be in a subform, or can it
be in the same form as the combobox?
-the combo box has two fields, only showing the second one
that has the text in it. The first hidden column is a
[TypeID] that I was trying to use in the code.


To filter a form,you do not need to alter the underlying query,
which contains all possible records. You just set the
form.filter property to the text of the where clause, and set
the form.filterOn property to true.

So your code becomes much simpler,

Private Sub combo1_AfterUpdate()
If combo1.Value = "1" Then 'Standard Procedures
ComboSortSQL = "tbl_Procedures.[TypeID] = 1"
me.filter = combofltrSQL
me.filterOn= true
elseif combo1.Value = 2 Then
ComboSortSQL = "tbl_Procedures.[TypeID] = 2"

etc...

Now if you want to order the records by a different column,
you would use the same technique and the form's orderby
property. You do not need an orderon.

If you want to return different columns, you would need to
generate the SQL and simply change the form's .recordsource
property

If combo1.Value = "1" Then 'Standard Procedures
me.recordsource = "Select * from tbl_Procedures"
elseif combo1.Value = 2 Then
me.recordsource = "Select * from tbl_Procedures
--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #3

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

Similar topics

3
7461
by: Wayne Wengert | last post by:
I am trying to populate a combobox with a lisy of items. I want the 1st item to be "Make a Selection" and the following items will be names from a table in my DB (See code below). When I run the...
4
9083
by: Beeeeeeeeeeeeves | last post by:
I have an ownerdrawn combo box which I am drawing with an image and some text, this is all working beautifully apart from the difference in the Brush I have to draw the background and the text with...
7
23519
by: NCrum | last post by:
I want to set the Default value of a Combobox for any changeable record and have got this working but it is totaly unsatisfactory see the code below I loop through the items in the Combo looking...
3
3012
by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
7
20750
by: charliewest | last post by:
Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this list to a ComboBox control using the "DataSource" property. I have set the DisplaySource and ValueMember properties as well....
0
2026
by: Doug | last post by:
This is a repost of an item that I still cannot resolve. I have 3 combo boxes. The first leads to the second to the third. When I have selected a value in the second box, the third box shows...
5
2130
by: ross kerr | last post by:
Hi All, I am extending the combobox to create a control that selects an item based on the text the user is typing into the text area of the control. I have an issue that occurs only when i...
30
4510
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
6
2839
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
3
7486
by: Simon Tamman | last post by:
I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and hope someone might have an idea about what's going...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
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
7183
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
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.