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

Can someone help me with some code involving lookup?

269 256MB
I tried to manipulate some code that was based on making two selections and then looking up the third. Now I have just one item to select and want my database to provide a lookup. I took the code (pasted at the end for reference) and came up with this:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cboSize_AfterUpdate()
  5. If Not IsNull(Me![cboSIZE]) Then
  6.   Me![txtCommodityNum] = DLookup("[BLANK_COMMODITY_NUM]", "tblBLANK_COMMODITY")
  7.  
  8. End If
  9. End Sub
  10.  
  11. Private Sub cboClass_Change()
  12.      Call ChangeRowSource
  13. End Sub
  14.  
  15. End Sub
  16.  
The code actually fires without bugs (which is a huge accomplishment for me) but it returns only one commodity number no matter what I enter. So it's not working. I suspect my error is at the end of the longest line...it had ticks and quotes that I deleted and those things get me every time.
Also, I researched online before asking the question, and the internet kept directing me to a wizard. I'm way too far in my DB to invoke that. Relationships are defined, tables, queries, and forms are made, etc.

Thanks in advance to the generous person who helps me!

Just for reference, here is the wonderful and working code I began with. cboCLASS no longer exists in new DB.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cboClass_AfterUpdate()
  5. If Not IsNull(Me![CBOclass]) Then
  6.   Me![cboSIZE].RowSource = "SELECT [SIZE] FROM tblCOMMODITY WHERE [CLASS] = '" & Me![CBOclass] & "' ORDER BY CInt(IIf(IsNumeric(Right([SIZE],1)),[SIZE],Left([SIZE],Len([SIZE])-1)));"
  7. End If
  8. End Sub
  9.  
  10. Private Sub cboSize_AfterUpdate()
  11. If Not IsNull(Me![CBOclass]) And Not IsNull(Me![cboSIZE]) Then
  12.   Me![txtCommodityNum] = DLookup("[COMMODITY_NUM]", "tblCOMMODITY", "[CLASS] = '" & Me![CBOclass] & _
  13.                        "' And [SIZE] = '" & Me![cboSIZE] & "'")
  14. End If
  15. End Sub
  16.  
  17. Private Sub cboClass_Change()
  18.      Call ChangeRowSource
  19. End Sub
  20.  
  21. Private Sub cboSize_Change()
  22.     Call ChangeRowSource
  23. End Sub
  24.  
  25. Private Sub ChangeRowSource()
  26.    With Me
  27.         If Not IsNull(.CBOclass) And Not IsNull(.cboSIZE) Then
  28.             .txtCommodityNum = DLookup("[COMMODITY_NUM]", _
  29.                                        "tblCOMMODITY", _
  30.                                        "[CLASS] = '" & .CBOclass & "' And " & _
  31.                                        "[SIZE] = '" & .cboSIZE & "'")
  32.         End If
  33.     End With
  34. End Sub
  35.  
  36.  
Jan 7 '13 #1

✓ answered by NeoPa

That's not quite what I was after Danica, but let me see if I can piece together what we do have and see if we can work from there. If I go astray then let me know where of course.

You are looking to find a record where some size field matches a size control on your form. There is only one criteria required and it must be selected in order to determine the commodity. From earlier posts I can determine (Again, correct if I misread.) that the control is called [cboSize] and the field is called [BLANK_SIZE]. You have also stated that you believe [BLANK_SIZE] to be a text field rather than numeric.

If all that is correct then the following code should do what you need :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cboSize_AfterUpdate()
  5.     Dim strWhere As String
  6.  
  7.     With Me
  8.         If IsNull(.cboSIZE) Then
  9.             .txtCommodityNum = Null
  10.         Else
  11.             strWhere = Replace("([BLANK_SIZE]='%S')", "%S", .cboSize)
  12.             .txtCommodityNum = DLookup(Expr:="[BLANK_COMMODITY_NUM]", _
  13.                                        Domain:="[tblBLANK_COMMODITY]", _
  14.                                        Criteria:=strWhere)
  15.         End If
  16.     End With
  17. End Sub

14 1869
DanicaDear
269 256MB
Not sure why my [code] inserts aren't working like I'm used to.
Jan 7 '13 #2
Seth Schrock
2,965 Expert 2GB
In answer to your second post, it is probably because your end insert has the slash as \ instead of / which is what is inserted when you use the <CODE/> button.

In answer to your first post, you are correct that the error lies in your longest line. DLookup can only return one value and always selects the first record that it finds. Since your DLookup() function doesn't have any criteria, it will always return the value in the first record of the table. You need to add a criteria so that it will find the record you want and then it should work.
Jan 7 '13 #3
DanicaDear
269 256MB
I feel like I am getting closer but still not there.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboSize_AfterUpdate()
  2. If Not IsNull(Me![cboSIZE]) Then
  3.   Me![txtCommodityNum] = DLookup("[BLANK_COMMODITY_NUM]", "tblBLANK_COMMODITY", [BLANK_SIZE] = '" & Me![cboSIZE] & "')
  4.  
  5. End If
  6. End Sub
Got any more advice? I'll not be shameful in admitting I don't fully understand ' " and Me! But I did look online for more help and I think I'm getting close. :-)
Jan 7 '13 #4
Seth Schrock
2,965 Expert 2GB
No need to be ashamed of not understanding. That is why we are here.

Try this in place of line 3:
Expand|Select|Wrap|Line Numbers
  1. Me!txtCommodityNum = DLookup("[BLANK_COMMODITY_NUM]", "tblBLANK_COMMODITY", "[BLANK_SIZE] = " & "'" & Me![cboSIZE] & "'")
I don't deal too much with text criteria like this, so I'm not totally sure on what is needed with the double and single quotes.

Me! just refers to the current form. After the bang (the exclamation point), you have control names such as txtCommodityNum. This is instead of the full reference Forms!form_name!txtCommodityNum

I feel like I have read an article about the use of double and single quotes, but I can't find it right now. Hopefully an expert can get in here and explain it to you (and me :)
Jan 7 '13 #5
DanicaDear
269 256MB
Thanks Seth. Not working yet. It's not calling the debugger, but it's not updating the field either. I know we're close. We can see if someone else's eye can catch it. I have plenty of time...I am always very grateful for the help I receive on Bytes.
Jan 7 '13 #6
Seth Schrock
2,965 Expert 2GB
Try this:

Expand|Select|Wrap|Line Numbers
  1. Me!txtCommodityNum = DLookup("[BLANK_COMMODITY_NUM]", "tblBLANK_COMMODITY", "[BLANK_SIZE] = '" & Me![cboSIZE] & "'")
I found a website that listed a DLookup with a text criteria and this is what their syntax was.
Jan 7 '13 #7
DanicaDear
269 256MB
Seth, thank you again. Tried that with same result. Code is firing but not updating my field. I can follow that matching it to the original code. So I wonder if I deleted too much from the original code. I found a reference to cbo_Class in my first pasting of the code. I deleted that too; now I'm down to this: (Perhaps I have oversimplified this?)
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cboSize_AfterUpdate()
  5. If Not IsNull(Me![cboSIZE]) Then
  6.   Me![txtCommodityNum] = DLookup("[BLANK_COMMODITY_NUM]", "tblBLANK_COMMODITY", "[BLANK_SIZE] = " & "'" & Me![cboSIZE] & "'")
  7.  
  8. End If
  9. End Sub
  10.  
  11. Private Sub cboSIZE_BeforeUpdate(Cancel As Integer)
  12.  
  13. End Sub
  14.  
Jan 7 '13 #8
NeoPa
32,556 Expert Mod 16PB
Hi Danica. Feel free to give me a call. I'd love to talk again anyway, and I'm up for hours yet anyway ;-)

Onto the issue. First off, Seth seems to be doing a decent job. Especially as he's still still learning much of it himself. So far all the advice has been good.

Let me start though, by getting you to take a step back and to ask yourself exactly what you require. How many items do you want to filter on, and what type are they all? String values and numeric values are handled quite differently in SQL (and SQL is what filters are written in). Here's an explanatory link - Quotes (') and Double-Quotes (") - Where and When to use them. That further links you to other references related to filtering (or WHERE clause specification).

You also need to know exactly which controls in your filter criteria are mandatory and which are optional.

Only when we know all of that detail can we advise properly on what you need.

PS. The link explains all about the quotes. Me, is a special object reference found in all Form and Report object modules that always refers to the associated object. So, in Form_frmA's object module Me refers to Form_frmA, whereas in Form_frmB's object module Me refers to Form_frmB. I hope that explains it clearly.
Jan 7 '13 #9
DanicaDear
269 256MB
Hi NeoPa! Can't call you tonight I'm afraid. It's the football national championship and my team is playing in the game! :-)

Oh, you have just reminded me how little I am. I don't know the answers to your questions but that has never stopped me before. LOL. On a brigher note, I do understand the Me explanation.
I *think* I am filtering on one item and nothing is numeric so I guess that means these items are strings. :-) You have never let my inability to answer your question stop me from getting the answer I seek. You have an amazing ability to keep on simplifying it until our intellects meet. Sorry you have to travel so far. LOL!! :-D
Hopefully we will talk soon.

Thanks to you AND Seth.
Jan 7 '13 #10
NeoPa
32,556 Expert Mod 16PB
Another night then Danica. I'll look forward to it, but I'm playing football tomorrow night myself (different football from yours I expect, but never mind). Good luck for tonight anyway. I hope your team wins :-)

As for your form, you refer in the OP to making two selections. I expect these are the controls we're talking about. What are the names of the controls, and the fields they are supposed to match? Are both necessary or could only one, or even neither, be selected in some circumstances. It seems they are both string values. That helps, but we need a little more before we can proceed.

We will be looking to produce a filter string that fits the following pattern :
Expand|Select|Wrap|Line Numbers
  1. ([Field1] = 'XXX') AND ([Field2] = 'YYY')
Sometimes not all of that will be necessary though, hence the greater details required.
Jan 7 '13 #11
DanicaDear
269 256MB
I understand the confusion. Great news. I *think* this is going to be very simple for your great mind. ;-)
The original piece of code was from a more complicated DB and it had two filters. However I stole the code because I lack the ability to write it, but can dangerously manipulate it, and what I'm aiming for NOW is one simple filter. I want to choose a size, and generate a commodity number.
Jan 7 '13 #12
NeoPa
32,556 Expert Mod 16PB
That's not quite what I was after Danica, but let me see if I can piece together what we do have and see if we can work from there. If I go astray then let me know where of course.

You are looking to find a record where some size field matches a size control on your form. There is only one criteria required and it must be selected in order to determine the commodity. From earlier posts I can determine (Again, correct if I misread.) that the control is called [cboSize] and the field is called [BLANK_SIZE]. You have also stated that you believe [BLANK_SIZE] to be a text field rather than numeric.

If all that is correct then the following code should do what you need :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub cboSize_AfterUpdate()
  5.     Dim strWhere As String
  6.  
  7.     With Me
  8.         If IsNull(.cboSIZE) Then
  9.             .txtCommodityNum = Null
  10.         Else
  11.             strWhere = Replace("([BLANK_SIZE]='%S')", "%S", .cboSize)
  12.             .txtCommodityNum = DLookup(Expr:="[BLANK_COMMODITY_NUM]", _
  13.                                        Domain:="[tblBLANK_COMMODITY]", _
  14.                                        Criteria:=strWhere)
  15.         End If
  16.     End With
  17. End Sub
Jan 7 '13 #13
DanicaDear
269 256MB
Yes yes. Magic again! SCORE!!
Jan 8 '13 #14
NeoPa
32,556 Expert Mod 16PB
You've been watching too much football my dear (or should that be Danica ;-)). Anyway, I'm always pleased to help :-)
Jan 9 '13 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: lawrence | last post by:
Can someone please tell me where I can get some open source code for resizing images? I know such code has been written a million times and I don't feel like doing it again from scratch.
1
by: Shi Jin | last post by:
Hi there, I am very new to C# and dot net. I am currently working on a web service and a client. Now I need to change the default class construction in the proxy code from this.Url = "some...
17
by: HALLES | last post by:
HELLO ! I seek to use a form too fill a file without putting a server on my computer. I am no C# programmer, i can do some things in Javascript but IT CAN T WRITE REWRITE APPEND A FILE ON LOCAL...
5
by: Chris Saunders | last post by:
I came across a construction in some code I was reading that I have never seen before and hope that someone could tell me how to interpret this: int (*literalScanners)(const ENCODING *, const...
1
by: Mark Friedman | last post by:
Does anyone know if there is a way to get some code in an ASP.NET application to run even before any HTTP requests come in for that application? I have some timer based code that I would like to...
10
by: Michael | last post by:
Hi Everyone. I have been designing a form with about 100 or so controls and today I pasted some code from another test project into this one and then all the controls on the form disapeared from...
10
by: Andrew Kidd | last post by:
I see this when I'm stepping through in the debugger ... just thought I'd ask, and I just know it's going to be one of those "Doh" moments, but it's got me foxed just now.
2
by: Ravi | last post by:
Can someone provide Code for vCARD Parser in C language ? I am in the need for vCard Parser which i can integarte on VC++ environment. Thanks in advance Ravindra Singhai
3
by: MalcR | last post by:
I’m a bit of a drag and drop merchant, but this one can’t be solved using the wizards. Would someone be kind enough to point me to some code which will allow me to resolve the following:- My table...
0
by: savage678 | last post by:
Hi Everyone, I am new to this forum and am i dire need of some help. I am trying to use wildcard searches in infopath. I have it connected to an access database using data connection. I have...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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,...
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...
0
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...

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.