Results 1 to 9 of 9
  1. #1
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211

    Set Temp Var from Data Macro

    Is there any way to set a TempVar from a data macro or Named macro? I want to store settings for my application, but I don't want a settings form. Currently, I use the Settings table and a bound Settings Form to show the settings from the table. Each setting is then assigned to a TempVar. I'm willing to keep the Settings table, but only want a single form, so don't want to include the Settings form.

    I thought of using a data macro when a Settings table record changes or a new record is added. If the data macro or named macro could set TempVars or run code, this would work for me. But these options are disabled in the macro designer. The macro designer will only allow setting LocalVars or ReturnVars. Most of my code relies on TempVars, which are retrieved from the Settings table at start-up time.



    A data macro would be perfect. I could click a button on my main form which would open the Settings table, and when I changed or added a setting into the table, the associated TempVar could be updated. This allows settings to be changed, and the changes would propagate between sessions since the table would be updated.

    I can't seem to figure out how to update the TempVars if my GetSettings function can't be run from data or named macros.

    Any suggestions on how to do this? Thanks...

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    I don't understand why you can't run code from a named macro...

    Click image for larger version. 

Name:	mcr.png 
Views:	22 
Size:	8.5 KB 
ID:	48672 Click image for larger version. 

Name:	vba.png 
Views:	22 
Size:	10.0 KB 
ID:	48673


    Click image for larger version. 

Name:	run.png 
Views:	23 
Size:	28.9 KB 
ID:	48674

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,906
    There is a SetTempVar macro action?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Dave,

    Thanks for the nice example.
    I used it to try a mock up as follows:

    Created 2 functions
    -fncMakeTempVars
    -GetSettings

    Created 1 Named Macro (based on DaveGri sample)
    -macTempVar

    RunCode Function Name fncMakeTempVars

    Used an existing table _ApplicationObjectList


    The only record in the table is:

    Id ObjType ObjName ObjLocation ObjCleanLocation ObjModLocation sysDateAdded
    1 2 DBObjects C:\Users\JP\AppData\Local\DbgSaveRaw\Form_DBObject s.txt C:\Users\JP\AppData\Local\DbgClean\Form_DBObjects. txt C:\Users\JP\AppData\Local\DbgWithDebug\Form_DBObje cts.txt 07-Sep-22 7:18:14 PM


    Here are the functions:

    Code:
      Option Compare Database
    Option Explicit
    
    ' ----------------------------------------------------------------
    ' Procedure Name: fncMakeTempVars
    ' Purpose: Mockup to get values from a table and assign these to Tempvars
    ' Procedure Kind: Function
    ' Procedure Access: Public
    ' Author: Jack
    '(Based on Davegri sample https://www.accessforums.net/showthread.php?t=86612&p=500258#post500258)
    ' Date: 09-Sep-22
    ' ----------------------------------------------------------------
    Function fncMakeTempVars()
              Dim tmpv1 As TempVars  'dim 2 tempvars
              Dim tmpv2 As TempVars
    10        GetSettings            'run a function to get some values
    20        TempVars!tmpv1 = Split(GetSettings, "|")(0) 'assign retrieved values to tempvars
    30        TempVars!tmpv2 = Split(GetSettings, "|")(1)
    40        MsgBox "TempVar1: " & TempVars!tmpv1 & vbCrLf & "TempVar2: " & TempVars!tmpv2
    End Function


    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: GetSettings
    ' Purpose: To retrieve some values from a table
    ' Procedure Kind: Function
    ' Procedure Access: Public
    ' Author: Jack
    ' Date: 09-Sep-22
    ' ----------------------------------------------------------------
    Function GetSettings()
              Dim a As String, b As String
    10        a = DLookup("ObjLocation", "_ApplicationObjectList")
    20        b = DLookup("ObjCleanLocation", "_ApplicationObjectList")
    30        GetSettings = a & "|" & b
    End Function



    The result of running the macro:
    Click image for larger version. 

Name:	MacroResult.PNG 
Views:	16 
Size:	7.4 KB 
ID:	48679


    May not be what OP needs, but should provide some insight.

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,404
    @Orange, Nice expansion on basic idea. OP also mentioned updating the table if his code reset any of the extracted tempvar values. That could be done with update queries to reset table field values with the new tempvar values.

  6. #6
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    Quote Originally Posted by davegri View Post
    I don't understand why you can't run code from a named macro...

    Click image for larger version. 

Name:	mcr.png 
Views:	22 
Size:	8.5 KB 
ID:	48672 Click image for larger version. 

Name:	vba.png 
Views:	22 
Size:	10.0 KB 
ID:	48673


    Click image for larger version. 

Name:	run.png 
Views:	23 
Size:	28.9 KB 
ID:	48674
    From the pictures, it looks like you are using the standard macro designer, which does indeed let you run code. The macro designer which opens when you have a table open and activate the Table ribbon is different. Either the "After" events macro buttons or the "Named Macro" button don't offer Set TempVar action, only LocalVar or ReturnVar options. I created a "RunCode" macro in the normal designer, which works fine. But that can't be called from any of the data or named macros. In your example, the macro you created is, I guess, a named macro, but it is not the same as the "Named Macro" available from the Table ribbon.

  7. #7
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    Quote Originally Posted by orange View Post
    Dave,

    Thanks for the nice example.
    I used it to try a mock up as follows:

    Created 2 functions
    -fncMakeTempVars
    -GetSettings

    Created 1 Named Macro (based on DaveGri sample)
    -macTempVar

    RunCode Function Name fncMakeTempVars

    Used an existing table _ApplicationObjectList


    The only record in the table is:

    Id ObjType ObjName ObjLocation ObjCleanLocation ObjModLocation sysDateAdded
    1 2 DBObjects C:\Users\JP\AppData\Local\DbgSaveRaw\Form_DBObject s.txt C:\Users\JP\AppData\Local\DbgClean\Form_DBObjects. txt C:\Users\JP\AppData\Local\DbgWithDebug\Form_DBObje cts.txt 07-Sep-22 7:18:14 PM


    Here are the functions:

    Code:
      Option Compare Database
    Option Explicit
    
    ' ----------------------------------------------------------------
    ' Procedure Name: fncMakeTempVars
    ' Purpose: Mockup to get values from a table and assign these to Tempvars
    ' Procedure Kind: Function
    ' Procedure Access: Public
    ' Author: Jack
    '(Based on Davegri sample https://www.accessforums.net/showthread.php?t=86612&p=500258#post500258)
    ' Date: 09-Sep-22
    ' ----------------------------------------------------------------
    Function fncMakeTempVars()
              Dim tmpv1 As TempVars  'dim 2 tempvars
              Dim tmpv2 As TempVars
    10        GetSettings            'run a function to get some values
    20        TempVars!tmpv1 = Split(GetSettings, "|")(0) 'assign retrieved values to tempvars
    30        TempVars!tmpv2 = Split(GetSettings, "|")(1)
    40        MsgBox "TempVar1: " & TempVars!tmpv1 & vbCrLf & "TempVar2: " & TempVars!tmpv2
    End Function


    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: GetSettings
    ' Purpose: To retrieve some values from a table
    ' Procedure Kind: Function
    ' Procedure Access: Public
    ' Author: Jack
    ' Date: 09-Sep-22
    ' ----------------------------------------------------------------
    Function GetSettings()
              Dim a As String, b As String
    10        a = DLookup("ObjLocation", "_ApplicationObjectList")
    20        b = DLookup("ObjCleanLocation", "_ApplicationObjectList")
    30        GetSettings = a & "|" & b
    End Function



    The result of running the macro:
    Click image for larger version. 

Name:	MacroResult.PNG 
Views:	16 
Size:	7.4 KB 
ID:	48679


    May not be what OP needs, but should provide some insight.
    Thanks Orange, but as I mentioned to davegri, this is not the "Named" macro as it appears in the Table ribbon when you have a table open. That one is severly limited in what you can do. Makes sense, as I believe data macros were created to mimic triggers in a true relational database, and wouldn't be expected to do what full macros can do.

  8. #8
    RMittelman is offline Advanced
    Windows 10 Access 2016
    Join Date
    Dec 2010
    Location
    Simi Valley, CA
    Posts
    211
    Quote Originally Posted by davegri View Post
    @Orange, Nice expansion on basic idea. OP also mentioned updating the table if his code reset any of the extracted tempvar values. That could be done with update queries to reset table field values with the new tempvar values.
    Hi davegri,
    Actually you have it backwards. What I was asking about was running code after the user opens the table and makes inserts or updates to any of the settings. My GetSettings function clears TempVars then re-creates them based on table data. I was hoping when I change the table data, it would trigger resetting the TempVars, which data macros don't seem to be able to do.

    On all my other databases, this works fine, because I open up a pop-up modal continuous form which is bound to the Settings table. When the form closes, my code reruns the GetSettings function.

    For this application, I want all code to be in a single form, so if needed I can simply copy the form into other databases and use them there.

    Since this particular database only has 4 settings, I simplified things and removed the Settings table and form entirely, and set the 4 TempVars in my GetSettings function.

    I've posted my sample application in this thread:
    https://www.accessforums.net/showthr...307#post500307

    Scroll down to the bottom and take the second file attached, "AccessCodeCompare2.zip".

    Thanks to all for your help!

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    RMittelman,

    I did an audit log demo database with data macros and named data macro in this thread/post.
    Again, it may not be what you need, but may offer some insight on use of a named Data macro.
    That is the only dabbling I have done with named and data macros.

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

Similar Threads

  1. Replies: 5
    Last Post: 09-19-2016, 01:35 PM
  2. temp tables
    By vicsaccess in forum Programming
    Replies: 7
    Last Post: 03-20-2016, 11:38 AM
  3. Replies: 8
    Last Post: 12-15-2014, 09:41 AM
  4. temp tbl
    By slimjen in forum Programming
    Replies: 9
    Last Post: 09-11-2014, 02:17 PM
  5. Replies: 2
    Last Post: 12-20-2011, 10:09 AM

Tags for this Thread

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