AutoExec Module not Initiating - ms-access

My data storage form has a new module (AutoExec) which is meant to test the connection to a SQL server db upon opening the form. Only it is not firing when opened. I was wondering if there was something in my code causing this, this is my first Module so I am not familiar with the proper forms yet.
Public Sub AutoExec()
Dim cnn As ADODB.Connection
Dim localrst As New ADODB.Recordset
Dim remoterst As New ADODB.Recordset
Set cnn = New ADODB.Connection
cnn.Open "Provider=SQLOLEDB; Data Source=DB\P003,49503; Initial Catalog=HRLearnDev;" _
& "User Id=USERNAME; Password=PASSWORD;"
If cnn.State = adStateOpen Then
MsgBox ("You have an established connection.")
Else
MsgBox ("Cannot connect to remote server. Data will be stored locally to CDData Table until application is opened again.")
End If
cnn.Close
Dim rst As New ADODB.Recordset
End Sub

When a database includes a macro named AutoExec, Access will run that macro at database startup.
In order for that to work, AutoExec must be an Access macro object. In your case, you have a VBA procedure named AutoExec. Since that is not a macro object, Access does not run the procedure automatically at database startup.
I suggest you create a VBA function ...
Public Function Startup()
Add the code from the body of your AutoExec procedure to the function.
Then create a new Access macro (an actual Access macro object --- on the ribbon in Access 2007, choose Create->Macro), use the macro RunCode action to run your new Startup() function. Name that macro object as AutoExec.
Here is a screenshot of my simple AutoExec example macro open in Design View.

Related

Converting Macros to VBA from Outside Current Database

I'd like a way to programatically convert Access macros to VBA modules. I know how to do this manually. I was working with some code to do this within the current database and I got it to work. However, I would like to be able to do this outside of the current database. I plan to have this program housed with other utility type programs in the same database. Below is my code. I am getting a message that says, "Microsoft Access cannot find the test1 you referenced in the Object Name arguement. Do you have any suggestions on how to acheive what I want to acheive?
Private Sub Command2_Click()
MsgBox "start here"
Dim strDB As String
Dim appAccess As Access.Application
Dim m As Object
strDB = "C:\Users\me\Desktop\testme.accdb"
Set appAccess = New Access.Application
appAccess.OpenCurrentDatabase strDB
For Each m In appAccess.CurrentProject.AllMacros
Debug.Print m.Name
DoCmd.SelectObject acMacro, m.Name, True
DoCmd.RunCommand acCmdConvertMacrosToVisualBasic
Next
Set appAccess = Nothing
End Sub
DoCmd is a member of the Application object. Ask those commands to work from the new Application object instance you created (appAccess) instead of the Application instance which is running your Command2_Click() procedure.
appAccess.DoCmd.SelectObject acMacro, m.Name, True
appAccess.DoCmd.RunCommand acCmdConvertMacrosToVisualBasic
In other words, basically do the same for DoCmd as you did with appAccess.OpenCurrentDatabase and appAccess.CurrentProject ... you associated them to the specific Application instance.

Why does an instance of Access quit when opened from VBA after the macro is finished running?

I am wondering if someone has encountered this before. When I create an instance of Access (2013) through VBA, the Access application quits when the macro is finished running even though I don't have any Application.quit statement anywhere in the subroutine. Any ideas?
Sub Test()
Dim axApp As Access.Application
Set axApp = CreateObject("Access.Application")
axApp.Visible = True
End Sub
The new Access application instance is created and referenced by the axApp object variable.
At the end of your procedure axApp goes out of scope and is destroyed. As a result of its destruction, the Access application instance it references is closed.
If you want that Access instance to stick around after your VBA procedure is done, set its .UserControl property to True.
Sub Test()
Dim axApp As Access.Application
Set axApp = CreateObject("Access.Application")
axApp.Visible = True
axApp.UserControl = True
End Sub

Can this Access VBA code be performed by an Access Macro?

I have successfully created a VBS file that Windows Task Scheduler can use to export data from my database, by triggering an Access macro. Now i need to import - most steps are simple and can be achieved in the Access macro but I need to re-create table links first (field names and positions often change in the source files and it seems to mess things unless i first delete and re-create the links.)
Here's the VBA code - is there a corresponding macro action that i have missed?
'delete and recreate links to Account and Company
Dim db As DAO.Database
' Re-link the CSV Table
Set db = CurrentDb
On Error Resume Next: db.TableDefs.Delete "Contact": On Error GoTo 0
db.TableDefs.Refresh
DoCmd.TransferText TransferType:=acLinkDelim, TableName:="Contact", _
FileName:="c:\db\contact.csv", HasFieldNames:=True
db.TableDefs.Refresh
On Error Resume Next: db.TableDefs.Delete "Account": On Error GoTo 0
db.TableDefs.Refresh
DoCmd.TransferText TransferType:=acLinkDelim, TableName:="Account", _
FileName:="c:\db\account.csv", HasFieldNames:=True
db.TableDefs.Refresh
db.Close
Set db = Nothing
Yes, the macro action you missed is called Run Code.
The only caveat is that Run Code can only call VBA functions. So you can't just paste your code in the macro. Instead, you need to put it into a VBA function in an MS Access module, like this:
Public Function ReCreateTableLinks()
'delete and recreate links to Account and Company
Dim db As DAO.Database
'... paste the rest of your code here
End Function
By the way, are you saying that you are using Windows Task Scheduler, which calls a VBS file, which opens the Access database and executes the macro?
Am I understanding this correctly?
If yes: you don't need the VBS file at all if it does nothing but open the Access database.
You can just execute the Access database directly with Windows Task Scheduler.
If you name the macro autoexec, it will execute automatically when the Access database opens.

Help Debugging this Code to Convert for .adp file type

I had a form with some VB code that was using Access 2003. Recently we wanted to use the same form as a small front end interface for another database that has a SQL Server backend. However, the file type for this project in Access is .adp and not all of the vb code is working properly. If you could help me fix the bugs in this code:
Private Sub SurveyNameCombo_AfterUpdate()
Dim db_CFC As DAO.Database
Set db_CFC = CurrentDb
Dim rst As DAO.Recordset, query As String, count As Integer
query = "SELECT DISTINCT SurveyID FROM tbl_SurveyMeta WHERE SurveyName = " & Chr(34) & Me.SurveyNameCombo.Value & Chr(34)
Set rst = db_CFC.OpenRecordset(query)
count = rst.RecordCount
If count > 1 Then
Me.SurveyIDCombo.RowSource = query
Else
rst.MoveFirst
Me.SurveyIDCombo.Value = rst.Fields(0).Value
Call SurveyIDCombo_AfterUpdate
End If
End Sub
It is throwing errors in the for the DAO.Database and DAO.Recordset.
Thank you for your help!
The error message "User-defined type not defined" on a line such as this ...
Dim db_CFC As DAO.Database
... means your application doesn't include a reference to the Microsoft DAO Object Library.
Open a code module, then check from the main menu in the VBE editor: Tools->References
Ordinarily the cure would be to place a check mark in the box next to Microsoft DAO Object Library, then click OK. However, your application is an ADP, and I don't know whether DAO can even be used in ADP. You can try. :-)
Sorry I can't tell you more. I quit using ADP a few years ago. Instead I use the MDB format with ODBC links to SQL Server database objects. Perhaps you could consider the same approach if you're unable to get the ADP version working as you need.

Block committing changes to Microsoft Access database

Is there a way to write a hook that will run before changes are committed to an Access DB? I'm looking for a way to block changes if a specific process is currently querying the DB.
You didn't give us much information to work with. Can you adapt something like Create and Use Flexible AutoNumber Fields so specific process first opens a table exclusively? Then any other operations which might change data would have to wait until they can lock that same table.
What is specific process? Do you have a method to determine when/if it is reading data from your database?
If specific process is external to the database, like web server ASP code which uses ADO to fetch data, you could see whether the ADO connection Mode and IsolationLevel properties can help.
Update: I used a form to experiment with the adModeShareDenyWrite Mode property for an ADO connection. While the form is open, other users can open the database, but not make any changes. However, if another user already has the db open when the form attempts to open the connection, it triggers an error.
Perhaps your deployment script could attempt to open a adModeShareDenyWrite connection, and bail out on error.
Option Compare Database
Option Explicit
Dim cn As ADODB.Connection
Private Sub Form_Close()
If Not (cn Is Nothing) Then
cn.Close
Set cn = Nothing
End If
End Sub
Private Sub Form_Load()
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=\\Cmpq\export\Access\backend\links2003.mdb;" & _
"User Id=admin;Password=;"
cn.Mode = adModeShareDenyWrite
cn.Open
End Sub