I have an MS Access database that creates physician report information. The final report has many pages consisting of all the physicians in my query.
My goal is to take this large report and move physician specific information into a separate folder as a pdf file. The large final report is master linked based on the physicians EPIC_ID. I also have a separate table that has distinct EPIC_ID with physician names (tblPhysicianID_Range). I want to go down the list of physicians from this table, and pull out specific information for each physician, create a pdf file of that information and send it to a folder with that physicians name.
At one point I had this code working just fine, but I had to make some adjustments to the large final report. None of the adjustments affected what links everything together, the EPIC_ID.
Exact error I'm getting:
Run-time error '3071':
This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try Simplifying the expression by assigning parts of the expression to variables.
And this is the highlighted debug:
DoCmd.OpenReport "rptCombined", acViewPreview, , _
strRptFilter, acHidden ' open the report hidden in preview mode setting the where parameter
Here is the entire code:
Private Sub Command0_Click()
Dim rst As DAO.Recordset
Dim strFolder1 As String, strFolder2
DoCmd.OpenQuery "qryPhysicianID_Range_tbl"
Set rst = CurrentDb.OpenRecordset("SELECT DISTINCT [Prov_Order_Name],[EPIC_ID] FROM [tblPhysicianID_Range] ORDER BY [EPIC_ID];", dbOpenSnapshot)
If rst.RecordCount > 0 Then ' make sure that we have data
rst.MoveFirst
Do While Not rst.EOF
strRptFilter = "[EPIC_ID] = " & Chr(34) & rst![EPIC_ID] & Chr(34)
strFolder1 = "U:\Co\Physician\Reappointment\Jordans Test\" 'common folder for files to go
strFolder2 = strFolder1 & rst.Fields("[Prov_Order_Name]") & "\" 'creates folder by Provider Name
If Dir(strFolder2, vbDirectory) = "" Then MkDir strFolder2 'determines if folder exists or not, and if it doesn't it makes one
DoEvents
rst.MoveNext
Loop
End If
If rst.RecordCount > 0 Then ' make sure that we have data
rst.MoveFirst
Do While Not rst.EOF
strRptFilter = "[EPIC_ID] = " & Chr(34) & rst![EPIC_ID] & Chr(34)
strFolder1 = "U:\Co\Physician\Reappointment\Jordans Test\" 'common folder for files to go
DoCmd.OpenReport "rptCombined", acViewPreview, , _
strRptFilter, acHidden ' open the report hidden in preview mode setting the where parameter
DoCmd.OutputTo acOutputReport, "rptCombined", acFormatPDF, _
strFolder1 & rst.Fields("[Prov_Order_Name]") & "\" & rst.Fields("[Prov_Order_Name]") & ".pdf" ' save the opened report
DoCmd.Close acReport, "rptCombined" ' close the report
DoEvents
rst.MoveNext
Loop
End If
rst.Close
Set rst = Nothing
End Sub
"And this is the highlighted debug: DoCmd.OpenReport "rptCombined", acViewPreview, , _ strRptFilter, acHidden ' open the report hidden in preview mode setting the where parameter"
This is pointing to the report, if the report is based on a select query, check the syntax of the query and ensure the query runs well by returning values.
Second thing you have to look at (this is based on the error message description in your post), the report filter has to be looked into.
if you share the images of the query design view and query SQL view, more clarity will come then, that can aid to a solution.
I was able to figure out the main problem why this wasn't working.
It turns out my "where" statement in the OpenReport section;
strRptFilter = "[EPIC_ID] = " & Chr(34) & rst![EPIC_ID] & Chr(34)
Wasn't working because my subreports weren't linked properly to the correct table [EPIC_ID] that was being used, and in addition, this was initially set up to find physician names, not ID's which are numbers/integer, so I switched the "where" to;
strRptFilter = "[EPIC_ID] = " & rst![EPIC_ID]
and it worked!
I now have a problem though, with having the subreports show their table in the final pdf even when there is no data for that physician, but I will post that as a separate question. Thanks!
Related
I realise this is probably the simplest question ever but I'm about to tear my hair out.
I am trying to export an access report for a single record in my data table, as specific by the field Unique ID.
I keep getting thrown a run time error occurring in the DoCmd.OpenReport line. For the life of me I cannot figure out where or why this is happening.
In a perfect world, I would want the user to be able to enter as many Unique ID's as they want, and it would export a single report for each line (not a large report containing all rows).
Sub test()
Dim MyFileName As String
Dim rst As DAO.Recordset
Dim strReportName As String ' report name to work with
Dim strOutPutFile As String ' pdf output file on disk
strReportName = "TEST"
Set rst = CurrentDb.OpenRecordset("table")
Do While rst.EOF = False
strOutPutFile = "C:\Users\me\Documents" & "-test1-" & rst![Unique ID] & ".pdf" ' output file = ID.pdf
DoCmd.OpenReport "TEST", acViewPreview , , "Unique ID = " & rst![Unique ID]
DoCmd.OutputTo acOutputReport, strReportName, acFormatPDF, strOutPutFile
DoCmd.Close acReport, strReportName
rst.MoveNext
Loop
rst.Close
MsgBox "done"
End Sub
I have a table that I need to format for use in a manual upload process. The table is thousands of rows so I need to use a repeatable process to quickly fix the way the data is given to me into what it needs to be. I have zero ability to control the way the data comes to me today. But, I have to format it to use it due to a system limitation. My current table is 4 columns, I need to output it as 3 columns. I have to group up by field names: "brand" and "promotion". Field name: "skus" I need to take and merge them into one continuous string by a single "brand" and "promotion" combination.
There are duplicate "promotion" by a given brand since they are created at a product level. But, the system they go into need to be "brand", "promotion", "skus".
Not really sure if I need to use VBA to do some of this inside Access. Or I can do this in two different queries.
You will need to use a bit of VBA to do this. The VBA will need to loop a recordset of data from the table that is filtered on brand and promotion and build up the sku string. Something like this perhaps:
Function fJoinData(strBrand As String, strPromotion As String) As String
On Error GoTo E_Handle
Dim db As DAO.Database
Dim rsData As DAO.Recordset
Dim strSQL As String
Set db = DBEngine(0)(0)
strSQL = "SELECT skus FROM Table1 " _
& " WHERE Brand='" & strBrand & "' " _
& " AND Promotion='" & strPromotion & "';"
Set rsData = db.OpenRecordset(strSQL)
If Not (rsData.BOF And rsData.EOF) Then
Do
fJoinData = fJoinData & ", " & rsData!skus
rsData.MoveNext
Loop Until rsData.EOF
End If
If Left(fJoinData, 2) = ", " Then fJoinData = Mid(fJoinData, 3)
fExit:
On Error Resume Next
rsData.Close
Set rsData = Nothing
Set db = Nothing
Exit Function
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "fJoinData", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume fExit
End Function
Rather than inserting into a table, I would suggest just creating a query which can then be exported:
SELECT DISTINCT
T.Brand,
T.Promotion,
fJoinData(T.Brand,T.Promotion) AS skus
FROM Table1 AS T
Regards,
I am creating an automated process that exports 50 tables I have in access to a CSV. The issue I'm having is one row in a table has carriage returns or line breaks mixed in. When I open the exported CSVs as a text file, some lines are broken up.
The data type for the troublesome row are all text. I need help integrating a statement in my VBA script to remove the spaces in front of any characters & remove any line breaks or carriage returns as well.
There is only one column in this one table giving me issues.
I have tried Trim & Replace statements that don't seem to work. It could be caused from not writing them correctly.
Option Compare Database
Option Explicit
Public Sub ExportDatabaseObjects()
On Error GoTo Err_ExportDatabaseObjects
Dim db As Database
Dim td As TableDef
Dim d As Document
Dim c As Container
Dim i As Integer
Dim sExportLocation As String
Set db = CurrentDb()
sExportLocation = "C:\File Path\"
For Each td In db.TableDefs 'Tables
If Left(td.Name, 4) <> "MSys" Then
CurrentDB.execute " UPDATE " & td.Name & " SET yourColumn='" & REPLACE(yourColumn, CHAR(13)+CHAR(10), "") & "' WHERE INSTR(1, yourColumn, CHAR(13)+CHAR(10))>0"
DoCmd.TransferText acExportDelim, , td.Name, sExportLocation & "Table_" & td.Name & ".csv", True
End If
Next td
Set db = Nothing
Set c = Nothing
MsgBox "All database objects have been exported as a csv file to " & sExportLocation, vbInformation
Exit_ExportDatabaseObjects:
Exit Sub
Err_ExportDatabaseObjects:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_ExportDatabaseObjects
End Sub
My VBA script successfully exports all tables in my access database into the folder I want. Now I need some help cleaning up the data before it goes into production.
For Each td In db.TableDefs 'Tables
If Left(td.Name, 4) <> "MSys" Then
CurrentDB.execute " UPDATE " & td.Name & " SET yourColumn='" & REPLACE(yourColumn, CHAR(13)+CHAR(10), "") & "' WHERE INSTR(1, yourColumn, CHAR(13)+CHAR(10))>0"
DoCmd.TransferText acExportDelim, , td.Name, sExportLocation & "Table_" & td.Name & ".csv", True
End If
Next td
This should help you find and replace bad characters
I'm having a problem getting a recordset to run from a query I created in an MS Access 2010 database. here is t he code I want to run:
Private Sub Command192_Click()
Dim recs As String
Dim param As Integer
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("UnitRec_Qry", dbOpenDynaset)
With rs
.MoveLast
.MoveFirst
While Not .EOF
recs = recs & vbNewLine & !Spara & " - " & !Rec
.MoveNext
Wend
End With
MsgBox (recs)
End Sub
What this should output is a message box with a number of records from the query in a list. I do this so I can gather this and a number of other records into a text file for copying and pasting into a separate system. At the moment, I'm running this code so I can place it all into a string variable.
My problem is that I'm getting the dreaded "Too Few parameters expected 1" error.
The query works, I've saved it into the database and tested it and I get the expected results.
I tried running the recordset with SQL:
Set rs = CurrentDb.OpenRecordset("SELECT UnitRecommend_tbl.URecID, UnitRecommend_tbl.Spara," _
& " UnitRecommend_tbl.Rec, UnitRecommend_tbl.SvyID" _
& " FROM UnitRecommend_tbl" _
& " WHERE ((UnitRecommend_tbl.SvyID) = [Forms]![SurveyRegister_frm]![SurveyID])" _
& " ORDER BY UnitRecommend_tbl.Spara;", dbOpenDynaset)
I get the same error
I ran it again but removed the "WHERE" statement and the code ran just fine, but gave me every record in the table. Not what I wanted.
So, the fields are OK because the data runs. When I debug the text the parameter in the SQL does show up as the right parameter, in this case, the number 4 which is an integer.
So I'm at a loss here, I've searched through the other posts here and I have tried these possible solutions (unless I missed something).
I also tried using dbopensnapshot as well, still no joy. Wondering if I'm using the right code here now.
Any help would be great.
Cheers
A parameter like [Forms]![SurveyRegister_frm]![SurveyID] doesn't get evaluated automatically if you open a recordset in VBA.
Use this function:
Public Sub Eval_Params(QD As DAO.QueryDef)
On Error GoTo Eval_Params_Err
Dim par As DAO.Parameter
For Each par In QD.Parameters
' This is the key line: Eval "evaluates" the form field and gets the value
par.Value = Eval(par.Name)
Next par
Eval_Params_Exit:
On Error Resume Next
Exit Sub
Eval_Params_Err:
MsgBox Err.Description, vbExclamation, "Runtime-Error " & Err.Number & " in Eval_Params"
Resume Eval_Params_Exit
End Sub
with a QueryDef object like this:
Dim QD As QueryDef
Dim RS As Recordset
Set QD = DB.QueryDefs("UnitRec_Qry")
Call EVal_Params(QD)
Set RS = QD.OpenRecordset(dbOpenDynaset)
Alternatively, you can run it with SQL in the VBA code by moving the parameter outside of the SQL string:
Set rs = CurrentDb.OpenRecordset("SELECT UnitRecommend_tbl.URecID, UnitRecommend_tbl.Spara," _
& " UnitRecommend_tbl.Rec, UnitRecommend_tbl.SvyID" _
& " FROM UnitRecommend_tbl" _
& " WHERE ((UnitRecommend_tbl.SvyID) = " & [Forms]![SurveyRegister_frm]![SurveyID] & ")" & _
& " ORDER BY UnitRecommend_tbl.Spara;", dbOpenDynaset)
In MS Access I need to back up all queries to a text file
Im able to do this with other Access objects fine, for example the following is a sample that backs up all reports to a text file
Dim oApplication
Set oApplication = CreateObject("Access.Application")
For Each myObj In oApplication.CurrentProject.AllReports
WScript.Echo "Report " & myObj.fullname
oApplication.SaveAsText acReport, myObj.fullname, sExportpath & "\" & myObj.fullname & ".report"
Next
Ive tried the following to backup all queries
For Each myObj In oApplication.CurrentData.AllQueries
WScript.Echo "Query " & myObj.fullname
oApplication.SaveAsText acQuery, myObj.Name, sExportpath & "\" & myObj.Name & ".query"
Next
However the resulting text file is the query output. Its definitely not the Query Definition that Im looking for.
To be clear here is an image of what Im trying to export to text
Does anyone have any ideas on how that can be accomplished?
Iterating through the QueryDefs should work for you
Dim def As DAO.QueryDef
Dim defCol As DAO.QueryDefs
Set defCol = CurrentDb.QueryDefs
For Each def In defCol
Debug.Print def.SQL
Next
How about this (requires 'Microsoft Scripting Runtime' checked under Tools|References in the VBA editor):
Dim Def As DAO.QueryDef
Def FSO As New Scripting.FileSystemObject, Stream As Scripting.TextStream
For Each Def In CurrentDb.QueryDefs
Set Stream = FSO.CreateTextFile(sExportpath & "\" & Def.Name & ".query")
Stream.Write(Def.SQL)
Stream.Close
Next
Alternatively, if you're using VBScript:
Dim Def, FSO, Stream
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each Def In oApplication.CurrentDb.QueryDefs
Set Stream = FSO.CreateTextFile(sExportpath & "\" & Def.Name & ".query")
Stream.Write(Def.SQL)
Stream.Close
Next
The value of the acQuery constant is 1. (AcObjectType Enumeration)
Perhaps your results are because the code is using 6 instead of 1. I don't know what should happen in that situation because none of the AcObjectType constants have a value of 6. Using Access VBA, when I tried SaveAsText with 6, something strange happened: the output file was created but Windows denied me permission to view its contents; shortly later, a dialog box appeared which looked like Access was looking for something on SQL Server ... although the query definition I was saving does not involve SQL Server. Strange stuff!