Updating a table cell value from global variable & vice-versa using VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BigLouie
    New Member
    • Jan 2016
    • 2

    Updating a table cell value from global variable & vice-versa using VBA

    I've designed a database that stores some info about people, and the view available to the user depends on their access level. I've got most of the database completed, but am now having trouble with some global variables.

    Long story short, the problem is that each "user" who accesses the database needs different levels of access to various forms, etc, and their name, etc needs to be stored in a way that it can be accessed by any form/report during their session.

    I had this working well with global variables until recently, and hours of internet searching when this failed have revealed this is a bad way to do this! I have now created a table "tblVariabl es" to store this information temporarily, and I'm after the VBA code allowing me to access the cell contents, and load it into a variable for use and manipulation on the form, then update/store it back in the cell when closing the form.

    I would really appreciate some help in finishing this off so that it works! For simplicity, there's a table of users (tblusers) with "username", "password", "accessleve l", "firstname" , "surname", "fullname" (first name + last name); another table (tblVariables) stores the global variables for the session in case there's an error and they get reset. My current code looks something like below, but is not working! The immediate window on the login form shows the correct variables, but as soon as it gets to the main menu, all the variables are lost, and nothing is updated in the table!

    Code:
    Public Sub VariablesUpdate()
    on error goto ErrorHandler
    dim rstVariables as recordset
    
    set rstvariables = currentdb.openrecordset("tblVariables")
    
    with rstvariables
       .movefirst
       !CurrentUN = gstrUN
       !CurrentUFN = gstrUFN
       !CurrentAL = gstrAL
       !CurrentSU = gstrSU
    end with
    
    rstvariables.close
    
    Errorhandler:
    End Sub
    Last edited by Rabbit; Jan 26 '16, 11:57 PM. Reason: Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    Just a heads up, Access is not the way to go if you are truly needing security.

    Now for your code. In this case, you are assigning the values in the variables and writing them to the table. If you are wanting to edit the first record in the table, then all you need to do is add .Edit right after the .MoveFirst and add .Update right before your End With.

    Also, please make sure to always copy and paste your code and not retype it. Otherwise, typos in your code get subconsciously fixed and we would never see the error. Also, please use the [CODE/] button to added code tags around your code so that it can be formatted properly.

    Comment

    • BigLouie
      New Member
      • Jan 2016
      • 2

      #3
      Thanks Seth! I am new to the forum, and my next question WAS going to be "how do you get that cool code snippet window everyone seems to have" but you've anticipated and answered that too! Surely it can't be as simple as that (and if it does, i'll feel like a numpty)! I'll give it a shot and let y'all know how it went!

      Comment

      Working...