Results 1 to 10 of 10
  1. #1
    Jen0dorf is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    UK
    Posts
    453

    Calling variable via button

    Hi



    had to take a sideways step on my database to add another search option.

    I wrote this code and saved it in a module called DeclareVariable

    Option Compare Database

    Code:
    Sub DeclareVariables()
    Dim varGraveNo As String
    varGraveNo = Me![GraveNo]
    
    End Sub
    I then created a form called frmPlotSearch with a text box called GraveNo

    Added a button to open another form - frmPlotandGraveNo - based on the query below.


    Code:
    SELECT tblDeceased.GraveID, tblDeceased.Mlink, tblDeceased.Plot, tblDeceased.GraveNo, tblDeceased.Forenames, tblDeceased.Surname, tblDeceased.Age, tblDeceased.YearOfDeath
    FROM tblDeceased
    WHERE (((tblDeceased.GraveNo)="varGraveNo"));
    My logic was the user opens the form types in a grave no say 22 pressed the button and the query runs filtering the results to only include grave no 22

    I've now after hours of looking realised I have not included creating my variables. How can I include the creation of the variable in my sequence or am I totaly off track

    thanks

    Ian

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    You just need to declare your variable as Public and place it in a Standard module.
    public gstrGraveNo as string

    Then you can set it from behind one of your forms or whatever.
    gstrGraveNo = Me![GraveNo]

    And retrieve it from anywhere
    msgbox gstrGraveNo

    If you are going to use a public variable, be sure to assign it to a default in the form or reports OnLoad event.
    gstrGraveNo = ""

    Just be aware that if your application experiences a Run Time exception, the variable will reset to empty. So you need to error trap.

  3. #3
    Jen0dorf is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    UK
    Posts
    453
    Thanks

    thats the project for today

    Ian

  4. #4
    Jen0dorf is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    UK
    Posts
    453

    public and gobal variables

    Hi

    working under my ethos of never just using someone elses code I have been searching the web for tutorials about defining and using global/private variables in Access.

    I can't seem to find any either at idiots level ( ME) or advanced. I can't even find a definition of the differences between a global and public variable

    can someone post some links

    thanks

    Ian

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862

  6. #6
    Jen0dorf is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    UK
    Posts
    453

    Unhappy variable not working

    Hi

    ok spent all day getting absolutely nowhere. Time for a bottle of wine ok 2 will do.

    Went back to the start and created a query based on one table

    as below
    SELECT tblDeceased.Plot, tblDeceased.GraveNo, tblDeceased.Forenames, tblDeceased.Surname, tblDeceased.YearOfBurial, tblDeceased.GraveID
    FROM tblDeceased;
    [/CODE]
    And of course it worked. I then created a form based on the query and that worked.

    I need/want to filter the query so that only records with a specific user entered GraveNo will display.

    As per suggestions I created a public variable in a module called

    Code:
    PublicDeclareVariable
    with the code
    Code:
    Option Compare Database
    Option Explicit
    Public varGraveNo As String
    I then created an empty form with a text box and a button.

    The button opens the form created using my unfiltered query.

    In my form with the button and text box I selected the form properties and in the when deactivated option used the code

    Code:
    varGraveNo = Me![GraveNo]
    Thinking this would set the public variable.

    Lastly I went to my first query and in the conditions added the variable

    giving
    SELECT tblDeceased.Plot, tblDeceased.GraveNo, tblDeceased.Forenames, tblDeceased.Surname, tblDeceased.YearOfBurial, tblDeceased.GraveID
    FROM tblDeceased
    WHERE (((tblDeceased.GraveNo)="varGraveNo"));

    However it does not work, it runs with no errors but it shows no results no matter what I input ie if I type 21 into my text box and press the button nothing is hown in the results screen.

    Off for a glass of the red stuff.

    Would appreciate any thoughts

    as always

    thanks

    Ian

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    So you will not be able to resolve the value of the variable directly in a Query Object or an SQL statement.

    The following is taking the literal text, "varGraveNo" and comparing it to the field, GraveNo.
    WHERE (((tblDeceased.GraveNo)="varGraveNo"));

    If the value of varGraveNo is equal to fifteen, you would want your SQL to look like
    WHERE (((tblDeceased.GraveNo)="fifteen"));

    I know this is not what you want. Just trying to show something here. One option is to incorporate a function. And I guess this is where you started but did not have the global variable set yet. You need both the variable and the Function and I apologize if I side tracked you there.
    Create a function that grabs the global variable.

    Code:
    public function fGraveNumber() as string
    fGraveNumber = varGraveNo
    end function
    Then you can use the function within your Query Object and an SQL statement. I believe you need to include the parenthesis when using function names in SQL. fGraveNumber() vs. fGraveNumber
    Code:
    WHERE (((tblDeceased.GraveNo)= fGraveNumber()));

  8. #8
    Jen0dorf is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    UK
    Posts
    453
    Hi

    I'm a couple of glasses down the road so I'll leave it till tomorrow;-)

    No you didn't side track me, it's me trying to grasp the concepts to get the results I need confusing everyone else.

    THanks for all the help

    Ian

    ps Malbec is very nice!!!

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    OK, let us know. And yeah, I am a fan of the reds, too. However, some of those >15% bottles can get pricey.

  10. #10
    Jen0dorf is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    UK
    Posts
    453
    Hi

    just got back to work after a spell under the weather!!

    Some kind person replied with the suggestion of using temporary variables

    Can't find the post but it solved all my problems!! Thanks to the unknown person

    In case anyone else requires info I found this free tutorial well worth a look if like me you need visual elucidation


    https://www.youtube.com/watch?v=FIYZjoleNT8

    cheers

    Ian





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

Similar Threads

  1. loading a button caption to a global variable
    By texanmike02 in forum Access
    Replies: 7
    Last Post: 11-14-2013, 11:36 AM
  2. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  3. Replies: 24
    Last Post: 02-20-2013, 09:46 PM
  4. calling multiple reports from single form button
    By lugnutmonkey in forum Reports
    Replies: 3
    Last Post: 01-02-2013, 02:28 PM
  5. Replies: 0
    Last Post: 08-10-2011, 11:59 AM

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