I have a access program that let's the users click a button that will generate a backlog report, and display it then output that report to a folder on this path:
S:\ALC MASTER\Month End Reports - 2015\Senior Management\2015 Backlog Report
But for some reason I keep getting a run time error even though the file is being outputted to that folder even with the error. I just want to know if there is a way to suppress that error.
Here is My code:
Dim MonthStart As Integer
Dim MonthEnd As Integer
For i = 1 To DLookup(DMax("[rsu]", "[RSU totals]", ""), "[RSU totals]", "") Step 1
If DCount("*", "[RSU totals]", "[rsu] = " & i) > 0 Then
If Date < "#8/01/" & Year(Now) & "#" Then
setDate = "7/01/" & Year(Now)
rptName = "Sales Forecast Detail - Jan-Jun"
Else
setDate = "13/01/" & Year(Now)
rptName = "Sales Forecast Detail - Jul-Dec"
End If
fileDirName = "S:\ALC MASTER\Month End Reports - " & Year(Now) & "\" & "Senior Management" & _
"\" & Year(Now) & " Backlog Report" & "\" & Format(DateAdd("m", -1, Date), "mm") & "-" & _
Format(DateAdd("m", -1, Date), "mmm") & " - " & Format(DateAdd("m", -1, setDate), "mmm") & ".pdf"
DoCmd.OpenReport rptName, acViewReport, , "[rsu] = " & i
Reports(rptName).ReportFooter.Visible = Flase
DoCmd.OutputTo acOutputReport, rptName, "PDF Format(*.pdf)", fileDirName, True
DoCmd.Close acReport, rptName
End If
Next
Seems that my problem was i was looping through reports that didn't exist. Taking out the For loop and the initial If statement worked.
Related
I am creating a database for contact information for research participants. In it, I have a form with a command button that is supposed to save 1 of 2 possible reports as a pdf for each participant based on the value of one of the text fields on the form (which is linked to a table where the field is automatically calculated as 0 or 2). Basically if the value of this field is 0 then I want the "UnsignedLetter" report saved for that participant or if the value of this field is 2, I want the "SignedLetter" report saved.
I've got most of it working except when it comes to selecting the correct report to save. FindFirst seems to be the closest to being successful but it's not quite right. When I click the button for the code below, the participants coded as 0 get both versions of the report and not just the "UnsignedLetter" version. The same does not happen with the participants coded as 2. For example, on a recordset of 5 people with 3 coded as 2 and 2 coded as 0, I get 3 correct "SignedLetter" pdf's, 2 correct "UnsignedLetter" pdf's, and an additional 2 incorrect "SignedLetter" pdf's. This is the code I'm working with:
Private Sub SaveLetters_Click()
Dim rs As DAO.Recordset
Dim sFolder As String
Dim sFile As String
On Error GoTo Error_Handler
sFolder = Application.CurrentProject.Path & "\"
Set rs = Me.RecordsetClone
With rs
.FindFirst "OncID = 2"
Do While Not .EOF
DoCmd.OpenReport "SignedLetter", acViewPreview, , "[ID]=" & ![ID],
acHidden
sFile = Nz(![UNumber], "") & "_signed" & ".pdf"
sFile = sFolder & sFile
DoCmd.OutputTo acOutputReport, "SignedLetter", acFormatPDF, sFile, , , ,
acExportQualityPrint
DoCmd.Close acReport, "SignedLetter"
.MoveNext
Loop
End With
With rs
.FindFirst "OncID = 0"
Do While Not .EOF
DoCmd.OpenReport "UnsignedLetter", acViewPreview, , "[ID]=" & ![ID], acHidden
sFile = Nz(![UNumber], "") & "_unsigned" & ".pdf"
sFile = sFolder & sFile
DoCmd.OutputTo acOutputReport, "UnsignedLetter", acFormatPDF, sFile, , , ,
acExportQualityPrint
DoCmd.Close acReport, "UnsignedLetter"
.MoveNext
Loop
End With
MsgBox "Letters Sent to File", vbOKOnly + vbInformation, "Task Completed"
Application.FollowHyperlink sFolder
Error_Handler_Exit:
On Error Resume Next
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Exit Sub
Error_Handler:
If Err.Number <> 2501 Then
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: cmd_GenPDFs_Click" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
End If
Resume Error_Handler_Exit
End Sub
I've been searching for 2 days at this point and I can't find any solutions. I've tried doing the 2 With rs's separately, using 1 With rs as after the Then portion of an If .NoMatch statement for the other, changing the calculation so it results in text and not a number, and anything else I can think of or find online. I feel like I'm within throwing distance of tears at this point and would really appreciate any help you guys have.
There is no need for FindFirst. Most of the code for both reports is the same. Use an If Then Else and a variable to select appropriate report and dynamically execute commands.
Dim sRpt As String
With Me.RecordsetClone
Do While Not .EOF
If !OncID = 0 Then
sRpt = "Unsigned"
Else
sRpt = "Signed"
End If
DoCmd.OpenReport sRpt & "Letter", acViewPreview, , "[ID]=" & ![ID], acHidden
DoCmd.OutputTo acOutputReport, , acFormatPDF, _
CurrentProject.Path & "\" & Nz(![UNumber], "") & "_" & sRpt & ".pdf"
DoCmd.Close
.MoveNext
Loop
End With
I can't seem to find any answers here for my specific problem. Maybe it's because I don't know what I'm looking for...
Anyway, I have an Access form that has about 30 checkboxes for each live facility we have and I need to run a query (which works perfectly) based on what boxes are ticked and a begin and end date which are also on the form.
This is the SQL I have (generated by the Query Wizard) that shows every single facility for every single date.
TRANSFORM Count(tblFilesProcessed.[File Name]) AS [CountOfFile Name]
SELECT tblFilesProcessed.[Fac Alpha], tblFilesProcessed.[File Date], Count(tblFilesProcessed.[File Name]) AS [Total Of File Name]
FROM tblFilesProcessed
GROUP BY tblFilesProcessed.[Fac Alpha], tblFilesProcessed.[File Date]
PIVOT tblFilesProcessed.[File Type];
I need to be able to pick and choose what I want the report to show. Right now it shows Facility01 all the way through Facility30 for dates going back to March. I need to be able to tick a box for Facility04, Facility 15, and Facility 20 for the past week. Or Facility 03, 21, 22, 23, 24.
What you will need to do is to use VBA to look at the controls on the form, and build up a modified SQL statement which you will then set to be the query's .SQL. Something like this:
Private Sub cmdQuery_Click()
On Error GoTo E_Handle
Dim strWhere As String
Dim strSQL1 As String
Dim strSQL2 As String
strSQL1 = "TRANSFORM Count(F.[File Name]) AS [CountOfFile Name] " _
& " SELECT F.[Fac Alpha], F.[File Date], Count(F.[File Name]) AS [Total Of File Name] " _
& " FROM tblFilesProcessed AS F "
strSQL2 = " GROUP BY F.[Fac Alpha], F.[File Date] " _
& " PIVOT F.[File Type];"
If Me!chkFac1 = True Then strWhere = strWhere & "'Fac1',"
If Me!chkFac2 = True Then strWhere = strWhere & "'Fac2',"
If Len(strWhere) > 0 Then
strWhere = " AND F.[Fac Alpha] IN(" & Left(strWhere, Len(strWhere) - 1) & ") "
End If
If IsDate(Me!txtFrom) Then
strWhere = strWhere & " AND F.[File Date]>=" & Format(Me!txtFrom, "\#mm\/dd\/yyyy\#")
End If
If IsDate(Me!txtTo) Then
strWhere = strWhere & " AND F.[File Date]<=" & Format(Me!txtTo, "\#mm\/dd\/yyyy\#")
End If
If Left(strWhere, 4) = " AND" Then
strWhere = " WHERE " & Mid(strWhere, 5)
End If
CurrentDb.QueryDefs("qryFilesProcessed").SQL = strSQL1 & strWhere & strSQL2
sExit:
On Error Resume Next
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "frmFilesProcessed!cmdQuery_Click", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
I would suggest that rather than using 30 checkboxes, a multi-select list box would be better, as it allows for future proofing - indeed, you could set its .RowSource to be the list of unique Facility values from the table.
I do have an Access form with a dynamic form filter using many controls as criterion. All work just fine excepted for my date range... Here is the piece of code I am using..
Private Sub Command4_Click()
Dim strWhere As String
Dim lngLen As Long
Const DMY = "\#dd\/mm\/yyyy\#"
'***********************************************************************
If Not IsNull(Me.PerPNum) Then
strWhere = "([PNum] = " & Me.PerPNum & ") AND "
End If
If Not IsNull(Me.PerPPro) Then
strWhere = strWhere & "([PPro] = " & Me.PerPPro & ") AND "
End If
If Not IsNull(Me.PerPRev) Then
strWhere = strWhere & "([PRev] = " & Me.PerPRev & ") AND "
End If
If Not IsNull(Me.PerDesi) Then
strWhere = strWhere & "([Uinit] = """ & Me.PerDesi & """) AND "
End If
If Not IsNull(Me.DateStart) Then
strWhere = strWhere & "([TaskStart] >= " & Format(Me.DateStart, DMY) & ") AND "
End If
If Not IsNull(Me.DateEnd) Then
strWhere = strWhere & "([TaskEnd] < " & Format(Me.DateEnd + 1, DMY) & ") AND "
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
MsgBox "No criteria", vbInformation, "Nothing to do."
Else
strWhere = Left(strWhere, lngLen)
Me.Debug.Value = strWhere
Me.Sub_Desi_Schedule.Form.Filter = strWhere
Me.Sub_Desi_Schedule.Form.FilterOn = True
End If
When I look at my "debuger" the WHERE criterion seems just fine.
Howerver I am still getting "Enter Parameter Value" and nothing is displayed even if reenter them.
When not using the date range on my form filters, everything runs smoothly.
Suggestions, ideas is more then welcome.
(please keep in mind that I am not a VB Guru........ So be kind :-) )
I think you may just be missing date wrappers "#".
Try:
"([TaskStart] >= #" & Format(Me.DateStart, DMY) & "#) AND ([TaskEnd] < #" & Format(Me.DateEnd + 1, DMY) & "#)
I inherited this Access Database for a client and was tasked with updating some things in it. Unfortunately I don't know much about VBA/Access. I keep receiving the error Runtime error 2465: Can't find the field "|1" referenced on the DoCmd.SendObject line. I've done a lot of searching and it's such a cryptic error I'm getting nowhere. Can someone explain to me what is wrong here? I'm assuming it's a syntax issue somewhere or I'm using variables wrong or something.
Private Sub cbCompleted_AfterUpdate()
If cbCompleted = -1 Then
tbCompleted = Date
tbCompleted.Locked = True
Doctor_Name.Locked = True
Department.Locked = True
Start_Date.Locked = True
Specialty.Locked = True
Doctor_.Locked = True
Taxonomy_.Locked = True
DepartmentCombo.Locked = True
UPIN_.Locked = True
SpecialtyCombo.Locked = True
Dim sd As String
Dim dn As String
sd = "" & [Start Date]
dn = "" & [Doctor Name]
DoCmd.SendObject acSendNoObject, , , "Tim,Keith,Yvonne,Sandy,susan#domain.org,Vicki#domain.org", "Tom,Barbara,Rachael,Penny,Troy,bernasue#domain.org", , "Doctor " & dn & " " & "Start Date:" & " " & sd, dn & " " & "is scheduled to start" & sd & vbNewLine & "NPI# :" & [NPI#] & vbNewLine & "Specialty: " & [Speciality#] & vbNewLine & "Department/Practice: " & [Department#] & vbNewLine & "Provider# for HR/Acctg: " & [Doctor#], True
Else
'[snip] Unlock all fields locked above
End If
End Sub
Thanks much
i am creating a .vbs file that should open access, and inside access a form call "Issue Details", but passing a parameter, meaning that if i have 10 issues in my "Issues" table a vbs file is created for each one and when clicked should open the right record(would be one ID for each record in the table). It is so far opening access and it is opening the form(Issue Details) but it is blank. What am i missing? Help, getting crazy here ... Check code below. The weird thing here is that if i double click it again it will refresh and open the right record without opening anymore windows.. How can i fix that? I dont want to do it twice :)
Public Sub sendMRBmail(mrbid)
DoCmd.OpenForm "Issue Details", , , "[ID] = " & mrbid
End Sub
Private Sub Create_Click()
On Error GoTo Err_Command48_Click
Dim snid As Integer
snid = Me.ID
Dim filename As String
filename = "S:\Quality Control\vbs\QC" & snid & ".vbs"
Dim proc As String
proc = Chr(34) & "sendMRBmail" & Chr(34)
Dim strList As String
strList = "On Error Resume Next" & vbNewLine
strList = strList & "dim accessApp" & vbNewLine
strList = strList & "set accessApp = createObject(" & Chr(34) & "Access.Application" & Chr (34)")" & vbNewLine
strList = strList & "accessApp.OpenCurrentDataBase(" & Chr(34) & "S:\Quality Control\Quality DB\Quality Database.accdb" & Chr(34) & ")" & vbNewLine
strList = strList & "accessApp.Run " & proc & "," & Chr(34) & snid & Chr(34) & vbNewLine
strList = strList & "set accessApp = nothing" & vbNewLine
Open filename For Output As #1
Print #1, strList
Close #1
Err_Command48_Click:
If Err.Number <> 0 Then
MsgBox "Email Error #: " & Err.Number & ", " & "Description: " & Err.Description
Exit Sub
End If
End Sub
This is what is inside a created vbs file
On Error Resume Next
dim accessApp
set accessApp = GetObject("S:\Quality Control\Quality DB\Quality Database.accdb")
accessApp.Run"sendMRBmail","231"
set accessApp = nothing
Thanks to all that made inputs, i already found the answer. I added acFormEdit at the end of my DoCmd and it worked, check below:
DoCmd.OpenForm "Issue Details", , , "[ID] = " & mrbid, acFormEdit