Results 1 to 10 of 10
  1. #1
    leftylee is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    6

    Use a variable in a DLOOKUP?

    I have a form that opens from a button on another form. It includes a field with a DLOOKUP of the record displayed on the first form. It works just fine, but I'd like to open this second form from two different first forms. So I need the forms! reference in the DLOOKUP to be different depending on the form it was opened from. Can I use a variable somehow for this?



    Can find my way around Access pretty well but when I get into VB or even SQL, things get complicated for me.

    Thanks

  2. #2
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481
    Try hiding a text box on your first form. Store the first forms reference there and then build a reference to it in your second form. I've done this in VB, but I think you can do it by building expressions in a fields control source.
    Hertfordkc

  3. #3
    leftylee is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    6
    Sorry but I don't understand. When I build a reference on the second form to the form from which it was open, won't it have to be forms![formname][fieldname]? If so, how does that help? I need the [formname] to vary depending on which form it was opened from.

  4. #4
    Stanggirlie is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2009
    Location
    Iowa
    Posts
    25
    Can you nest the Dlookup in an IF statement? Just a thought.

  5. #5
    leftylee is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    6
    Seems like it should. Would the logic be something like "if form1 is open, then do this lookup, else do that lookup"? What expression would I use to test whether a form is opened?

  6. #6
    Stanggirlie is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2009
    Location
    Iowa
    Posts
    25
    Okay, I re-read your problem and this is how I understand it...
    You have Form1 and Form2. From these two different forms, you have Form3 that can be opened from either Form1 or Form2. And if it's opened from Form1, the DLookup is different then if it's opened from From2, correct?

    If that is the issue, I'd make a button on both Form1 and Form2. When you click that button, it does two things in order...
    First, it opens Form3
    Second, it calculates the DLookup in the manner you want

    The button then dictates the correct Dlookup you want. You don't have to worry about which form is open. This can be accomplished thru macros using the conditions. Does that make sense?

  7. #7
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    In VB, you can reference Parent.HiddenField...

    Quote Originally Posted by hertfordkc View Post
    Try hiding a text box on your first form. Store the first forms reference there and then build a reference to it in your second form. I've done this in VB, but I think you can do it by building expressions in a fields control source.
    Hertfordkc
    Sorry, I left out the most important part. From the third form, reference the field using "Parent.HiddenField......."

    Hertfordkc

  8. #8
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Code:
    If SysCmd(acSysCmdGetObjectState, acForm, "<formname1>") = -1 Then
         <perform first dlookup>
    elseIf SysCmd(acSysCmdGetObjectState, acForm, "<formname2>") = -1 Then
         <perform second dlookup>
    endif

  9. #9
    leftylee is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    6
    Rpeare, I think this is what I'm looking for. But where do I put this code? I want the result of the lookup to display in an unbound text box. Is the code the source for the text box?

  10. #10
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    you would put it in the ON OPEN property of the each of the first two forms can open.

    As I understand it you can have this

    FORM 1 ----> Button Click ----> FORM 3
    FORM 2 ----> Button Click ----> FORM 3

    And you want to pass a variable from either form 1 or form 2 (whichever is currently open) to form 3 so in the ON OPEN property of form 3 you would have to determine which form was open (the getobjectstate portion of the code) and do whatever you want to do based on the result of that if statement. Note I haven't done testing with this but I think it should work.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. DLookUp Help
    By ThaGreenMoose in forum Forms
    Replies: 10
    Last Post: 06-15-2011, 12:08 PM
  2. Dlookup in query using Global variable
    By newwales in forum Access
    Replies: 1
    Last Post: 06-03-2011, 03:47 PM
  3. Help Using Variable in DLookup Statement
    By bcmarshall in forum Access
    Replies: 9
    Last Post: 12-02-2010, 12:44 AM
  4. Replies: 4
    Last Post: 08-05-2010, 01:26 PM
  5. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums