Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 4th, 2006, 05:39 AM
Authorized User
 
Join Date: Sep 2003
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default passing variables through a combo box

hi all,

i am wondering how to pass the id of a combo box item to an sql statement.

I have a cb populated with a list of departments, pulled from the departments table. i want to display the name of the department in the cb, but i want also to be able to pass the id of the selected item so that i can update that particular entry.

could someone help me out with this? i have the cb filled with the data, I used the wizard to get it to pull both the id and the dept name from the table, and hide the id, but i dont know how to reference the id from within the cb.

thanks

Patrick

Visit my site: http://www.drybonesuk.com
__________________
Visit my site: http://www.drybonesuk.com
 
Old January 4th, 2006, 08:55 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

There are at least two ways to do this:

1. In the query for the recordsource of the form or report that you are opening from the form with the combo box, add criteria to the department field in the query like -

[Forms]![frmYourFormName].[cboDeptComboBox]

   When the form or report opens, the query will pull the bound field from your combo box.

2. Use the following code for the button that opens your form or report:

Dim stDocName As String
Dim lgDeptID As Long (or integer)
Dim stLink As String

lgDeptID = Me.cboYourComboBoxName
stLink = "[DeptID] = " & lgDeptID '(since this is numeric and not a string, single quotes are not needed here)

stDocName = "YourFormOrReportName"
DoCmd.OpenFormORReport stDocName, , stLink '(in the WHERE parameter)

HTH


mmcdonal
 
Old January 4th, 2006, 08:56 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

P.S. If you were passing a string to the query, you would use the following syntax:

"[Department] = " & "'" & stDeptName & "'"


mmcdonal
 
Old January 13th, 2006, 09:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Scenario 1:

1) Your combobox has two columns.
2) First column has the ID; its width is zero (invisible)
3) Second column has the dept. name; it's not invisible
4) The bound column is the first one, i.e. ID.
5) Your combobox name is cboMyComboBox.

In Scenario 1, the bound column is the first one, i.e. Column(0). So, to reference the ID is simply: Me.cboMyComboBox, because Me.cboMyComboBox defaults to Me.cboMyComboBox.Column(0) anyway.

Scenario 2:

1) Your combobox has two columns.
2) First column has the ID; its width is zero (invisible)
3) Second column has the dept. name; it's not invisible
4) The bound column is the SECOND one, i.e. Dept.
5) Your combobox name is cboMyComboBox.

In Scenario 2, the bound column is the second one, i.e. Column(1). So to reference the ID is Me.cboMyComboBox.Column(0), because Me.cboComboBox defaults to Me.cboMyComboBox.Column(1).

Using Me.cboComboBox without the Column() property always refers to the bound column.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division





Similar Threads
Thread Thread Starter Forum Replies Last Post
Combo box to display items from parent combo box Gini Visual Studio 2008 0 June 18th, 2008 12:30 AM
Count in combo box(display results in text box) mboyisis Access 4 April 4th, 2008 07:08 AM
Combo box choice creating filtered combo box stevensj5 Access 11 September 13th, 2007 11:33 AM
passing array from combo box thru ajax amorphic8 BOOK: Professional Ajax ISBN: 978-0-471-77778-6 0 July 10th, 2006 03:23 AM
Using values from a combo box as variables in SQL SoC Classic ASP Basics 7 July 20th, 2004 03:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.