473,396 Members | 1,879 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,396 software developers and data experts.

How to Pass Value from a Combo Box as Variable in Function

I am learning how to use simple functions to make my apps more powerful
and efficient.

On one screen, I want to populate field B: ItemDescription by looking
up the ItemDescription in the Items Table. I would use the key to the
Items Table from the selection the user made in the combo box.

SO: User chooses an item from the combo box whose value is "3". I
want to look up the ItemDescription of the Item with the Key "3" and
show that to the user in another field at the bottom of the form.

Here's what I have:

The control Source of the field ItemDescription is:
=fcnGetItemDescrip([cboGarment].[Column](0))
(The combo box is named cboGarment and the ItemKey is hidden in the
first column. The combo box is populating properly.)

The problem (I think) is that lngCustItemID is null. When I ask for its
value in the immediate window, I get NULL. Same as when I hover over
lngCustItemID in the code.

However, if I put another field on the form and have as its control
source the value of Column 0, it and the field ItemDescription both
populate properly.

Public Function fcnGetItemDescrip(lngCustItemID) As String

' Returns the Item Description based on the custItemID selected
fcnGetItemDescrip = DLookup _
("[ItemDescription]", "[tblCustomerItems]", "[CustItemID]= " &
lngCustItemID)
End Function

Thanks very much
Sara

Jan 22 '06 #1
1 6050
Hi Sara. I take it that the purpose of this is a learning exercise.

In a real database, you would not have an ItemDescription field in your
orders table, unless you wanted the user to be able to type into this text
box so they could:
a) enter a description for something that is not a product, or
b) describe a "widget" as a "watzit" for this particular order.

The problem with the code is probably a timing one. After you select an item
in the combo, Access does not update the calcuated field. You can force it
to do so, with:
Forms![NameOfYourFormHere].Recalc
At this point, the calculated field based on your function should call the
function again.

BTW, the name lngCustItemID suggests that this is a Long, but to get a Long,
you would need to code:
Public Function fcnGetItemDescrip(lngCustItemID As Long) As String
That would fail when the combo is Null, because a Long in VBA cannot be
Null. You need the argument to be a Variant, so it would be better to use:
Public Function fcnGetItemDescrip(varCustItemID As Variant) As String
When the value is Null, the 3rd argument in your DLookup() expression
becomes just:
[CustItemID]=
which does not make sense to Access, so you will get an error.
To avoid that, test it before the DLookup() line:
If Not IsNull(varCustItemID)
or at least supply something for null in the 3rd argument:
"[CustItemID] = " & Nz(varCustItemID, 0)

Of couse, if you were not practicing with code, you could just use a Control
Source of:
=DLookup("[ItemDescription]", "[tblCustomerItems]", "[CustItemID]= " &
Nz([cboGarment],0))

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"sara" <sa*******@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I am learning how to use simple functions to make my apps more powerful
and efficient.

On one screen, I want to populate field B: ItemDescription by looking
up the ItemDescription in the Items Table. I would use the key to the
Items Table from the selection the user made in the combo box.

SO: User chooses an item from the combo box whose value is "3". I
want to look up the ItemDescription of the Item with the Key "3" and
show that to the user in another field at the bottom of the form.

Here's what I have:

The control Source of the field ItemDescription is:
=fcnGetItemDescrip([cboGarment].[Column](0))
(The combo box is named cboGarment and the ItemKey is hidden in the
first column. The combo box is populating properly.)

The problem (I think) is that lngCustItemID is null. When I ask for its
value in the immediate window, I get NULL. Same as when I hover over
lngCustItemID in the code.

However, if I put another field on the form and have as its control
source the value of Column 0, it and the field ItemDescription both
populate properly.

Public Function fcnGetItemDescrip(lngCustItemID) As String

' Returns the Item Description based on the custItemID selected
fcnGetItemDescrip = DLookup _
("[ItemDescription]", "[tblCustomerItems]", "[CustItemID]= " &
lngCustItemID)
End Function

Thanks very much
Sara

Jan 23 '06 #2

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

Similar topics

5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
14
by: bill | last post by:
Can someone please show me an example of passing a string value into an sql statement in vb 2005? Something like this is what I'm after: Dim sqlButton1 As String = "Select * from tblAssets where...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.