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

Run an AutoExec Macro where an ODBC password is required

27
I want to update my database every night at 10pm. I have a Macro that runs all of the queries needed to update the database (which is also saved as a VBA module) and an AutoExec Macro to runs the "Update Inventory" macro. I also scheduled the task to run every night a 10pm. The problem i have is that in order to run the "Update Inventory" macro I have to enter my ODBC password and there is no way to save the password. This there a Macro action or VBA code I could add that would enter my password automatically?

Thanks
Cindy
Aug 24 '09 #1

✓ answered by Stewart Ross

Hi Cindy. Passwords will not normally be saved when you set up an ODBC connection. To do so would bypass first-level user security, so it really will make no difference whether or not you set up a user DSN - this will not store it for you.

In terms of the function I posted, its job when called is to reset the Connection string of one of the Data Access Object (DAO) table definitions to include the Username and Password for the ODBC link, then to refresh the link, and finally to restore the non-password version of the connection string (for security reasons once again). When Access connects to an ODBC database the link only has to be made once - it is then persisted for all other table accesses that use the current open connection.

DAO is the native form of Access data object. An alternative already suggested in the other post referred to by ADezii is to establish an ADO connection, but if DAO appears complicated then the ADO version will appear more so.

In both cases all that is happening is establishing a connection to the linked database via a VBA function call. That function has to establish the connection without manual intervention, typically by supplying the DSN information, including any defined Username and Password - the things you otherwise do manually via an ODBC dialogue box when trying to access a linked table in Access.

I'm sorry to say that it is difficult to simplify a process much further when it will always involve establishing a programmatic connection to another database, supplying the connection with the Username etc. As you may well have to adapt and modify code to meet your own needs you have to have a reasonable grounding in what the code does and how it achieves it to do so.

The function I supplied has a loop in it which is not needed if you will only be refreshing one table (lines 20, 26, 27). The loop refers to data which is obtained from a recordset (variable RS - lines 16, 19, 20, 21, 22, 23, 28) which again could be done away with if you only want to refresh one table.

However, for maintainability I would caution against hard-coding usernames and passwords into VBA code - as any change to a DSN, username or password would require a code change. That is why I store such data in a table, to allow easy maintenance.

Below is a simplified version of the function - but you need to supply a tablename, username and password to the function as parameters when it is called.

Expand|Select|Wrap|Line Numbers
  1. Public Function fODBCTablesRefresh(byVal strTableName as String, byVal strUserName as String, byVal strPassword as String ) As Boolean 
  2.     ' This function refreshes the DAO ODBC table connections 
  3.     ' for the linked database in which strTableName is any of the tables linked. 
  4.     '   
  5.     ' This function is used to automate the initial refresh of the ODBC table connection 
  6.     ' so that updates etc can run without user intervention to supply the UID and PWD components 
  7.     ' of the DB connection. 
  8.     ' 
  9.     ' 23/4/09 
  10.     ' 
  11.     On Error GoTo refresh_err 
  12.     Dim daoDB As DAO.Database 
  13.     Dim tdef As DAO.TableDef 
  14.     Dim strConnectOld As String 
  15.     Set daoDB = CurrentDb 
  16.     Set tdef = daoDB.TableDefs(strTablename) 
  17.     strConnectOld = tdef.Connect 
  18.     tdef.Connect = strConnectOld & "; UID=" & strUserName & "; PWD=" & strPassword 
  19.     tdef.RefreshLink 
  20.     tdef.Connect = strConnectOld 'restore no-password version of connect string 
  21.     fODBCTablesRefresh = True 
  22.     Exit Function 
  23. refresh_err: 
  24.     fODBCTablesRefresh = False 
  25. End Function 
-Stewart

8 18737
Megalog
378 Expert 256MB
Dont you have the option of storing the password for the ODBC connection when you initially create it? I dont remember ever having to re-enter passwords for my ODBC connections in the past.

As far as filling in the password box, look up the SendKeys function.. maybe that's the route you'll need to take.
Aug 24 '09 #2
kini113
27
Unforunately ODBC passwords can't be saved from what I've read on other sites due to registry security. I tried the SendKey() function but it didn't work maybe it's just not in the right place to respond to the prompt for the password. Here is the what I have so far

Expand|Select|Wrap|Line Numbers
  1. Function Update_Inventory()
  2. On Error GoTo Update_Inventory_Err
  3.  
  4.     DoCmd.SetWarnings False
  5.     ' Remove barstock on order from control inventory table
  6.     DoCmd.OpenQuery "Remove On Order", acViewNormal, acEdit
  7.     ' makes update table
  8.     DoCmd.OpenQuery "Update Inventory MT", acViewNormal, acEdit
  9.     SendKeys "PASSWORD {ENTER}", False
  10.     ' Gets actual bar value
  11.     DoCmd.OpenQuery "Make Bar Value Cost", acViewNormal, acEdit
  12.     ' Updates unit price to actual value of bar
  13.     DoCmd.OpenQuery "Update Unit Price", acViewNormal, acEdit
  14.     ' Adds the new inventory
  15.     DoCmd.OpenQuery "Add New Inventory", acViewNormal, acEdit
  16.     ' Adds split OD and ID
  17.     DoCmd.OpenQuery "UPDATE OD AND ID", acViewNormal, acEdit
  18.     ' updates changed quantites
  19.     DoCmd.OpenQuery "Update Qty", acViewNormal, acEdit
  20.     ' adds sorting tool for Mat'l spec
  21.     DoCmd.OpenQuery "Mat'l Spec Sort", acViewNormal, acEdit
  22.     ' Changes zero bar qty to null
  23.     DoCmd.OpenQuery "update zero qty", acViewNormal, acEdit
  24.     ' add zeroed bars into archive
  25.     DoCmd.OpenQuery "ADD Bar's at Zero", acViewNormal, acAdd
  26.     ' deletes zeroed bars from inventory
  27.     DoCmd.OpenQuery "Remove Zero bars", acViewNormal, acEdit
  28.     ' Adds Barstock On Order
  29.     DoCmd.OpenQuery "Bar Stock On Order", acViewNormal, acEdit
  30.     ' step 2
  31.     DoCmd.OpenQuery "DUPLICATE ON ORDER", acViewNormal, acEdit
  32.     ' step 3
  33.     DoCmd.OpenQuery "Filter On Order", acViewNormal, acEdit
  34.     ' step 4
  35.     DoCmd.OpenQuery "SORT MAT'L SPEC", acViewNormal, acEdit
  36.     ' step 5
  37.     DoCmd.OpenQuery "ADD STOCK ON ORDER", acViewNormal, acEdit
  38.     DoCmd.SetWarnings True
  39.     DoCmd.OpenReport "Bar Stock Inventory plus On Order", acViewPreview, "", "", acNormal
  40.  
  41.  
  42. Update_Inventory_Exit:
  43.     Exit Function
  44.  
  45. Update_Inventory_Err:
  46.     MsgBox Error$
  47.     Resume Update_Inventory_Exit
  48.  
  49. End Function
Thanks
Cindy
Aug 24 '09 #3
ajalwaysus
266 Expert 100+
But haven't you setup the ODBC connection on your PC?

copy and past this in your run window:
%SystemRoot%\system32\odbcad32.exe

that will display your ODBC connections on your PC, then you need to find your DSN and edit it to save your password.

-AJ
Aug 24 '09 #4
ADezii
8,834 Expert 8TB
@kini113
Have the AutoExec Macro execute a Public Function where the ODBC Password is programmatically entered into the Connection String. See Related Thread, Post #2:
http://bytes.com/topic/access/answer...e-certain-time
Aug 24 '09 #5
Stewart Ross
2,545 Expert Mod 2GB
I run an automated update and export at 5am each day. One of the linked databases is connected via ODBC, and the link is refreshed automatically by the following function, which makes use of the DAO connection string. The function is run from the On-Load event of the main switchboard form, which in turn is opened automatically by an Autoexec macro as mentioned by ADezii. I arrange it this way to be able to run updates etc manually as well as automatically, and to test additions and so on before they are added to the auto export lists running at 5am.

Expand|Select|Wrap|Line Numbers
  1. Public Function fODBCTablesRefresh() As Boolean
  2.     ' This function refreshes the DAO ODBC table connections
  3.     ' listed in the qryODBCRefreshList query
  4.     ' which in turn lists the first table to be connected for each different
  5.     ' connection string listed in table tblDatabasesConnected. 
  6.     '
  7.     ' This function is used to automate the initial refresh of the ODBC table connection
  8.     ' so that updates etc can run without user intervention to supply the UID and PWD components
  9.     ' of the DB connection.
  10.     '
  11.     ' 23/4/09
  12.     '
  13.     On Error GoTo refresh_err
  14.     Dim daoDB As DAO.Database
  15.     Dim tdef As DAO.TableDef
  16.     Dim RS As DAO.Recordset
  17.     Dim strConnectOld As String
  18.     Set daoDB = CurrentDb
  19.     Set RS = CurrentDb.OpenRecordset("qryODBCRefreshList")
  20.     Do While Not RS.EOF
  21.         Set tdef = daoDB.TableDefs(RS![tablename])
  22.         strConnectOld = RS![Connect String]
  23.         tdef.Connect = strConnectOld & "; UID=" & RS![USER ID] & "; PWD=" & RS![CONNECTION PASSWORD]
  24.         tdef.RefreshLink
  25.         tdef.Connect = strConnectOld 'restore no-password version of connect string
  26.         RS.MoveNext
  27.     Loop
  28.     RS.Close
  29.     fODBCTablesRefresh = True
  30.     Exit Function
  31. refresh_err:
  32.     fODBCTablesRefresh = False
  33. End Function
  34.  
Although at present I refresh just one ODBC-linked database I keep the names of all of the databases connected and their connection strings etc in a table within the DB called tblDatabasesConnected. There is an associated query called qryODBCRefreshList referred to in the function which returns only the ODBC ones from tblDatabasesConnected. It supplies the name of the first linked table for that database, the connection string, the UID, and PWD (as stored in table tblDatabasesConnected).

The refresh function needs to know the name of one of the tables to refresh, which as mentioned I obtain in qryODBCRefreshList. I do this by joining to another query which returns the names of the ODBC-linked tables. How it is done does not matter, what does is that the refresh function has to be supplied the name of a table, a connection string, a UID and a PWD. I do this using a recordset and loop as there can be several ODBC databases to refresh as developments progress.

If you are always going to refresh just one database it would be possible to adapt the function above by doing away with the use of the recordset and associated loop, supplying the name of a table to refresh and the UID and PWD strings as parameters.

-Stewart
Aug 25 '09 #6
kini113
27
@ajalwaysus
The ODBC connection is a System DSN and I can't find anywhere to save it. Let me know if I'm missing something. Is there a way to change it to a User DSN I know I can save a password there?
Stewart thanks for your info, but I must admit i'm really new to VBA and I don't understand alot of what you said. I normally work in MS Access syntax, is there anyway you could explain your previous post more simply.


Thanks
Cindy
Aug 25 '09 #7
Stewart Ross
2,545 Expert Mod 2GB
Hi Cindy. Passwords will not normally be saved when you set up an ODBC connection. To do so would bypass first-level user security, so it really will make no difference whether or not you set up a user DSN - this will not store it for you.

In terms of the function I posted, its job when called is to reset the Connection string of one of the Data Access Object (DAO) table definitions to include the Username and Password for the ODBC link, then to refresh the link, and finally to restore the non-password version of the connection string (for security reasons once again). When Access connects to an ODBC database the link only has to be made once - it is then persisted for all other table accesses that use the current open connection.

DAO is the native form of Access data object. An alternative already suggested in the other post referred to by ADezii is to establish an ADO connection, but if DAO appears complicated then the ADO version will appear more so.

In both cases all that is happening is establishing a connection to the linked database via a VBA function call. That function has to establish the connection without manual intervention, typically by supplying the DSN information, including any defined Username and Password - the things you otherwise do manually via an ODBC dialogue box when trying to access a linked table in Access.

I'm sorry to say that it is difficult to simplify a process much further when it will always involve establishing a programmatic connection to another database, supplying the connection with the Username etc. As you may well have to adapt and modify code to meet your own needs you have to have a reasonable grounding in what the code does and how it achieves it to do so.

The function I supplied has a loop in it which is not needed if you will only be refreshing one table (lines 20, 26, 27). The loop refers to data which is obtained from a recordset (variable RS - lines 16, 19, 20, 21, 22, 23, 28) which again could be done away with if you only want to refresh one table.

However, for maintainability I would caution against hard-coding usernames and passwords into VBA code - as any change to a DSN, username or password would require a code change. That is why I store such data in a table, to allow easy maintenance.

Below is a simplified version of the function - but you need to supply a tablename, username and password to the function as parameters when it is called.

Expand|Select|Wrap|Line Numbers
  1. Public Function fODBCTablesRefresh(byVal strTableName as String, byVal strUserName as String, byVal strPassword as String ) As Boolean 
  2.     ' This function refreshes the DAO ODBC table connections 
  3.     ' for the linked database in which strTableName is any of the tables linked. 
  4.     '   
  5.     ' This function is used to automate the initial refresh of the ODBC table connection 
  6.     ' so that updates etc can run without user intervention to supply the UID and PWD components 
  7.     ' of the DB connection. 
  8.     ' 
  9.     ' 23/4/09 
  10.     ' 
  11.     On Error GoTo refresh_err 
  12.     Dim daoDB As DAO.Database 
  13.     Dim tdef As DAO.TableDef 
  14.     Dim strConnectOld As String 
  15.     Set daoDB = CurrentDb 
  16.     Set tdef = daoDB.TableDefs(strTablename) 
  17.     strConnectOld = tdef.Connect 
  18.     tdef.Connect = strConnectOld & "; UID=" & strUserName & "; PWD=" & strPassword 
  19.     tdef.RefreshLink 
  20.     tdef.Connect = strConnectOld 'restore no-password version of connect string 
  21.     fODBCTablesRefresh = True 
  22.     Exit Function 
  23. refresh_err: 
  24.     fODBCTablesRefresh = False 
  25. End Function 
-Stewart
Aug 26 '09 #8
NeoPa
32,556 Expert Mod 16PB
A new (hijack) post has been split into its own thread (Link Refresh Code). Any interested parties may visit to help.
Sep 14 '09 #9

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

Similar topics

7
by: Tom Chidwick | last post by:
Hello, I've custom-built a fairly extensive object model that is designed to run in the background and control the opening and closing of related forms. The whole thing kicks-off with an...
4
by: mvivar | last post by:
Hi everybody: This will be not easy to explain as my mother language is not english, so my apologies in advance if it sounds confusing. We have a database access 97 wich controls time of...
6
by: MLH | last post by:
Did A97, perchance, do away with the need for an AutoExec macro to initiate an autoexec sequence for databases? Or, must we still suffer with that hideous macro?
4
by: Bob Sanderson | last post by:
I am trying to set up an ODBC DSN to our company MySQL database so that I can use a MS Access front end. I am using the MySQL ODBC 3.51 driver. The database is running off of Apache on our Windows...
9
by: Rysiek | last post by:
Hi. I've got a "base.mdb" secured with "system.mdw". There's also a password set on "base.mdb" . After configuration ODBC driver ( system DSN ), supplying user and his pass too, when I try to...
6
by: darren via AccessMonster.com | last post by:
Hi I have multiple .mde FE's linked to an .mdb BE. The FE is distributed with an Auto FE updater and as such all users are on the same version of the FE. However, one user is getting "Action...
5
by: dick | last post by:
I can't seem to get AutoExec to run a function. Per an article on this group, I: 1) built a boolean function in a module that simply issues a MsgBox, then returns True 2) in AutoExec macro, I...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
4
by: kini113 | last post by:
I want to update my MS Access database every night at 10pm. I have a Macro that runs all of the queries needed to update the database (which is also saved as a VBA module) and an AutoExec Macro to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.