GPSPOW
asked on
Passing variables from Excel Module to Access Module
I have two VBA modules.
the first is in Excel and creates two date variables and then calls an Access Macro (which runs a Function Module) which creates a table based on the Date Variables.
When I run the Excel module the Access Query runs but the dates do not pass over and the query hangs up.
Here are the two modules. Can you tell me what I can do to correct this?
Excel Module:
Private Sub RunCreateTable()
Dim strDb As String
Dim appAccess As Object
Dim ModName As String
Dim startdt As String
Dim enddt As String
startdt = "#" & Format$(DateSerial(2013, 4, 1), "mm/dd/yyyy") & "#"
enddt = "#" & Format$(DateSerial(2013, 7, 31), "mm/dd/yyyy") & "#"
ModName = "mcrCreate"
strDb = "P:\Accounting\NHSN.accdb"
Set appAccess = CreateObject("Access.Appli cation")
With appAccess
.OpenCurrentDatabase strDb
.DoCmd.RunMacro ModName
.Quit
End With
Set appAccess = Nothing
End Sub
Access Module:
Option Compare Database
Function CreateNHSNTbl()
DoCmd.SetWarnings False
Dim MyDb As DAO.Database
Dim qdef As DAO.QueryDef
Dim SQL As String
Dim strWhere As String
Set MyDb = CurrentDb()
MyDb.QueryDefs.Delete "qry_NHSN_tbl"
strWhere = " WHERE(((dbo_AbstractData.P atientClas s) <> 'IN')) AND (dbo_SchAppointmentOrOpera tions.Desc ription IS NOT NULL) "
strWhere = strWhere & "GROUP BY dbo_SchOrPatCases.SurgeonN ame, dbo_AbstractData.Name, dbo_AbstractData.BirthDate Time, dbo_AbstractData.UnitNumbe r, "
strWhere = strWhere & "dbo_SchAppointments.DateT ime, dbo_SchAppointmentOrOperat ions.Descr iption, dbo_SchOrPatCases.VisitID, dbo_AbstractData.AccountNu mber "
strWhere = strWhere & "HAVING (dbo_SchAppointments.DateT ime)betwee n " & startdt & " and " & enddt & ";"
SQL = "SELECT dbo_SchOrPatCases.SurgeonN ame AS Surgeon, dbo_AbstractData.Name AS PTName, dbo_AbstractData.BirthDate Time AS DOB, "
SQL = SQL & "dbo_AbstractData.UnitNumb er AS [MedRec#], dbo_SchAppointments.DateTi me AS ProcDate, dbo_SchAppointmentOrOperat ions.Descr iption AS ProcDescr "
SQL = SQL & " into tblNHSNProcedures "
SQL = SQL & " FROM (((((dbo_SchOrPatCases LEFT JOIN dbo_AbsOperationProcedures ON dbo_SchOrPatCases.VisitID = dbo_AbsOperationProcedures .VisitID) "
SQL = SQL & "LEFT JOIN dbo_AbstractData ON dbo_SchOrPatCases.VisitID = dbo_AbstractData.VisitID) LEFT JOIN dbo_AdmVisitOrders "
SQL = SQL & "ON dbo_SchOrPatCases.VisitID = dbo_AdmVisitOrders.VisitID ) LEFT JOIN Sheet1 ON dbo_AbsOperationProcedures .Procedure Code = Sheet1.ProcedureCode) "
SQL = SQL & "LEFT JOIN dbo_SchAppointments ON dbo_AbstractData.VisitID = dbo_SchAppointments.VisitI D) LEFT JOIN dbo_SchAppointmentOrOperat ions "
SQL = SQL & "ON dbo_SchAppointments.Appoin tmentID = dbo_SchAppointmentOrOperat ions.Appoi ntmentID "
SQL = SQL & strWhere
Set qdef = MyDb.CreateQueryDef("qry_N HSN_tbl", SQL)
DoCmd.OpenQuery "qry_NHSN_tbl"
End Function
I tested the Access Module with the date variables within the Access Module and it works fine. The end product is to reference cells in Excel for the dates.
thanks
glen
the first is in Excel and creates two date variables and then calls an Access Macro (which runs a Function Module) which creates a table based on the Date Variables.
When I run the Excel module the Access Query runs but the dates do not pass over and the query hangs up.
Here are the two modules. Can you tell me what I can do to correct this?
Excel Module:
Private Sub RunCreateTable()
Dim strDb As String
Dim appAccess As Object
Dim ModName As String
Dim startdt As String
Dim enddt As String
startdt = "#" & Format$(DateSerial(2013, 4, 1), "mm/dd/yyyy") & "#"
enddt = "#" & Format$(DateSerial(2013, 7, 31), "mm/dd/yyyy") & "#"
ModName = "mcrCreate"
strDb = "P:\Accounting\NHSN.accdb"
Set appAccess = CreateObject("Access.Appli
With appAccess
.OpenCurrentDatabase strDb
.DoCmd.RunMacro ModName
.Quit
End With
Set appAccess = Nothing
End Sub
Access Module:
Option Compare Database
Function CreateNHSNTbl()
DoCmd.SetWarnings False
Dim MyDb As DAO.Database
Dim qdef As DAO.QueryDef
Dim SQL As String
Dim strWhere As String
Set MyDb = CurrentDb()
MyDb.QueryDefs.Delete "qry_NHSN_tbl"
strWhere = " WHERE(((dbo_AbstractData.P
strWhere = strWhere & "GROUP BY dbo_SchOrPatCases.SurgeonN
strWhere = strWhere & "dbo_SchAppointments.DateT
strWhere = strWhere & "HAVING (dbo_SchAppointments.DateT
SQL = "SELECT dbo_SchOrPatCases.SurgeonN
SQL = SQL & "dbo_AbstractData.UnitNumb
SQL = SQL & " into tblNHSNProcedures "
SQL = SQL & " FROM (((((dbo_SchOrPatCases LEFT JOIN dbo_AbsOperationProcedures
SQL = SQL & "LEFT JOIN dbo_AbstractData ON dbo_SchOrPatCases.VisitID = dbo_AbstractData.VisitID) LEFT JOIN dbo_AdmVisitOrders "
SQL = SQL & "ON dbo_SchOrPatCases.VisitID = dbo_AdmVisitOrders.VisitID
SQL = SQL & "LEFT JOIN dbo_SchAppointments ON dbo_AbstractData.VisitID = dbo_SchAppointments.VisitI
SQL = SQL & "ON dbo_SchAppointments.Appoin
SQL = SQL & strWhere
Set qdef = MyDb.CreateQueryDef("qry_N
DoCmd.OpenQuery "qry_NHSN_tbl"
End Function
I tested the Access Module with the date variables within the Access Module and it works fine. The end product is to reference cells in Excel for the dates.
thanks
glen
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
This worked perfectly after I loaded the DAO library.
Thanks
Thanks
ASKER
Dim qdef As DAO.QueryDef
These two statements are giving me the error:
User-defined type not defined