VBA run function in an access db via another access db - vba

all I need to run a function that will run multiple reports in different access databases... ad DB to rule them all :)
here the structure: in my multi_report.accdb I have a form with a button that is running this code:
Private Sub Comando1_Click()
Dim db_report As String
Dim objAccess As Object
Dim ctr As Date
'TICKETS
db_report = "\\sdocenco01\OPC\11_SCL\TICKETS\ticket.accdb"
Set objAccess = GetObject(db_report) 'opens other db
objAccess.Visible = True
objAccess.Run "report_tickets" 'runs function in other db
objAccess.Quit 'quits other db
Set objAccess = Nothing
'PROCESSI
db_report = "\\sdocenco01\OPC\11_SCL\PROCESSI\utilita.accdb"
Set objAccess = GetObject(db_report)
objAccess.Visible = True
objAccess.Run "report_processi"
objAccess.Quit
Set objAccess = Nothing
...
in the line
objAccess.Run "report_tickets"
i get a run-time error '2517' routine report_tickets not found
I tried different codes like
objAccess.Run "ticket.report_tickets"
or
objAccess.Run "ticket.Database1.report_tickets"
but nothing seems to work
in the \sdocenco01\OPC\11_SCL\TICKETS\ticket.accdb i have this code:
Public Function report_tickets()
DoCmd.SetWarnings False
Dim cdb As DAO.Database, qdf As DAO.QueryDef
Set cdb = CurrentDb
...
End Function

First, according to the Docs, the project name must be included:
The name of the Function or Sub procedure to be run. If you are
calling a procedure in another database, use the project name and the
procedure name separated by a dot in the form:
"projectname.procedurename"
Next, I guess the "Report_" prefix confuses. Try renaming the functions and call them like:
objAccess.Run "YourProjectName.ReportTickets"

Related

Transfer Spreadsheet to Access Database with VBA

I have a browse button and pick and place the file name and path in textbox5. I need to use the same value in my file name but it does not work. It throws:
Run Time Error 2522- The action or method requires a File Name argument
Private Sub Command10_Click()
Dim dbs As DAO.Database
Dim td As DAO.TableDef
Dim fileName As String
'set current database
Set dbs = CurrentDb
Me.Text5 = fileName
DoCmd.TransferSpreadsheet acImport, , "tblS3DimportTemp", fileName, True
MsgBox "Data Uploaded!"
End Sub
Instead of: Me.Text5 = fileName
write:
fileName = Me.Text5
In many programming languages the left variable gets the value of the right one.

Converting VBA to VBScript - Not working but no error

I've been following articles and questions about converting VBA to VBScript but I'm now stuck. The following code still works in VBA (if I remove the Sub routine call) but it won't run as a script.
The code opens a connection to SQL Server to check a table to see if the process has already run today and loads the result into a Recordset. If the field is set to No then it opens up an Excel workbook and runs a macro. It works in VBA but when I run the same code as a script nothing happens (no errors either).
Can you see what the problem is? Thanks very much.
NB. There are two lines for cmd.CommandText. The commented out line is designed to always return No for testing purposes only.
' Author Steve Wolstencroft
' Inititates the Automated Excel Refresh Procedure
Option Explicit
Pivot_Refresh
Public Function ConnectToSQLDwarfP()
On Error Resume Next
ConnectToSQLDwarfP = "Driver={SQL Server Native Client 10.0};Server=DwarfP;Database=DwarfPortable;Trusted_Connection=yes;"
End Function
Public Sub Pivot_Refresh()
On Error Resume Next
Dim cnx
Dim Rst
Set cnx = New ADODB.Connection
cnx.ConnectionString = ConnectToSQLDwarfP
cnx.Open
Dim cmd
Set cmd = New ADODB.Command
cmd.ActiveConnection = cnx
cmd.CommandType = adCmdText
cmd.CommandText = "Select Case When max(DwarfPortable.dbo.fn_GetJustDate(pl.StartDateTime)) = DwarfPortable.dbo.fn_GetJustDate(getDate()) Then 'Y' Else 'N' End as RunToday From ProcessControl.dbo.ProcessLog pl Where pl.ProcessName = 'Excel_Auto_Refresh'"
'cmd.CommandText = "Select Case When max(pl.StartDateTime) = DwarfPortable.dbo.fn_GetJustDate(getDate()) Then 'Y' Else 'N' End as RunToday From ProcessControl.dbo.ProcessLog pl Where pl.ProcessName = 'Excel_Auto_Refresh'"
Set Rst = cmd.Execute
Dim objXL, objBook
Set objXL = CreateObject("Excel.Application")
If Rst.Fields("RunToday") = "N" Then
Set objBook = objXL.Workbooks.Open("\\nch\dfs\SharedArea\HI\Clinical-Informatics\InfoRequestOutputs\Regular-Jobs\Pivot-Refresh\Pivot-Refresh-Control.xls", 0, True)
objXL.Application.Visible = True
objXL.Application.Run "'Pivot-Refresh-Control.xls'!Auto_Refresh"
objXL.ActiveWindow.Close
objXL.Quit
Set objBook = Nothing
Set objXL = Nothing
End If
End Sub
You can't instantiate external objects in VBScript with e.g. New ADODB.Connection because there are no references to external libraries.
You can't use constants like adCmdText either. They will be treated as undefined empty variables.
You don't get any errors because you shut them up with On Error Resume Next. Remove that and you will get your errors.
Make sure all external object instantiation is done with CreateObject like you are doing for Excel, and replace all external constants with their literal values.

Object Required Error When Declaring String Variable

I am fetching a set of names from a database query and then reformatting it to a comma separated list. As I am using this functionallity a few Places in my app, I try to write it as a function getting the sql-query and returning the string.
Public Function String_from_query(StrSQL As String) As String
Dim dbs As Database
Dim rs As Recordset
Set dbs = CurrentDb
Dim results As String
results = ""
Set rs = dbs.OpenRecordset(StrSQL)
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do While Not rs.EOF
If results > "" Then
results = results & ", "
End If
results = results & rs("Navn")
rs.MoveNext
Loop
End If
Set String_from_query = results
End Function
This is then called from an event handler:
Private Sub Detalj_Format(Cancel As Integer, FormatCount As Integer)
Dim StrSQL As String
StrSQL = "SELECT Personer.Navn FROM Personer INNER JOIN Personoppgaver ON Personer.Initialer = Personoppgaver.Initialer WHERE Personoppgaver.Oppgaveid =" & Me.Oppgaveid.Value
Me.Tekst52.Value = String_from_query(StrSQL)
End Sub
If I have the code from the String_from_query function within the event handler and then directly assigns Me.Tekst52 to results, everything works fine. When I refactor the code as shown, I get a "Compile Error, Object required" when I try to run it and a marker on the last line in the sub. (Set String_from_query = results). I am not able to see what is wrong. Any help?
The keyword Set is only required when assigning variables to an Object. For Access, this would be Forms, Reports, Recordsets, etc. Or other Objects outside of Access (FileSystemObject, for example).
When setting strings, dates, numbers, etc, you do not need Set.
You can surmise this from the error message as well, Object required.

Initiating CurrentDb object at the form level

I have many Subs in a form with the following code
Dim db As DAO.Database
Set db = CurrentDb()
Dim strSQL as String
db.Execute strSQL
Is it good practice to initialize db at the "form level" i.e. write first two lines of code above only once? If so, how should I do it?
This is somewhat different from MSAccess: Global vs Local Variable Declaration
I have this function in a Module:
Public Function currDB() As Database
Static currDB_v As Database
If currDB_v Is Nothing Then
Set currDB_v = CurrentDb
End If
Set currDB = currDB_v
End Function
And call CurrDB.Execute
If you have no error handling as you posted in Q, then you can also write function:
Public Sub ExecSQL(SQL as string)
CurrDB.Execute SQL
End Sub

.openrecordsource "run-time error '91'"

I'm having an issue with the following lines of code, I feel its something simple but I can't put my finger on it. I get a Run-time error '91' object variable or with block variable not set error. The error happens on the rsc.OpenRecordSet... line. BadgeV is a number, I have the DAO reference installed, It is pulling from a linked sql server table where I have a primary key and identity. What am I missing?
Function FNC_Scan()
Dim db As DAO.Database
Dim rsc As DAO.Recordset
Set db = CurrentDb()
rsc.OpenRecordset ("SELECT dbo_ScanData.CardID, dbo_ScanData.Complete FROM dbo_ScanData WHERE (((dbo_ScanData.CardID)= Forms![Scan]!BadgeV))) AND ((dbo_ScanData.Complete)=0));")
If rsc.EOF Then
MsgBox "new item"
Else
MsgBox "Append Item"
End If
Set rsc = Nothing
Set db = Nothing
End Function
Should be
set rsc = db.OpenRecordset(...)
instead of
rsc.OpenRecordset(...)