473,383 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Save to Multiple Reports to PDF and AutoName using DLookup

Brilstern
208 100+
Q: I am getting "invalid use of null" error. Whats wrong?!?

The goal in this command is to first create a individual report "rptECR" for each SSN in tblBand Members. Next, save each report as a PDF and name each PDF the expression [location] and save into selected folder strMonth.

tblBand Members
Expand|Select|Wrap|Line Numbers
  1. Field                      Type
  2.     Rank                       Text
  3.     FName                      Text
  4.     LName                      Text
  5.     MI                         Text
  6.     SSN                        Text PK
  7.     MOS                        Text
  8.     EAS                        Date/Time
  9.     Instrument                 Text
  10.     Date Checked In            Date/Time
  11.     Supply Rep                 Text
qryMember
Expand|Select|Wrap|Line Numbers
  1. Field                      Type
  2.         Rank                       Text
  3.         FName                      Text
  4.         LName                      Text
  5.         MI                         Text
  6.         SSN                        Text PK
  7.         MOS                        Text
  8.         EAS                        Date/Time
  9.         Instrument                 Text
  10.         Date Checked In            Date/Time
  11.         Supply Rep                 Text
  12.         Location: [rank] & " " & [lname] & ", " & [fname]
Archive Button
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdArchive_Click()
  2.  
  3. Dim cdb As DAO.Database
  4. Dim strFilter As String
  5. Dim strCurrentPath As String
  6. Dim strReportFile As String
  7. Dim strReport As String
  8. Dim strFilter2 As String
  9. Dim strInputFileName As String
  10. Dim strmonth As String
  11. Set cdb = CurrentDb
  12. strFilter = "[SSN] = ' & !SSN & '"
  13. strmonth = Me.MonthSelect
  14. strCurrentPath = Application.CurrentProject.Path
  15. strReportFile = DLookup(Expr:="[location]", Domain:="[qrymember]", Criteria:=strFilter)
  16. strReportFile = Replace(strCurrentPath & "\strmonth & ' ECR'\%N.PDF", "%N", strReportFile)
  17. strReport = "rptECR"
  18.  
  19.    With cdb.TableDefs("[tblBand Members]").OpenRecordset(dbOpenTable)
  20.    Do While Not .BOF And Not .EOF
  21.      Call DoCmd.OpenReport(ReportName:="rptECR", View:=Access.AcView.acViewReport, WhereCondition:=strFilter)
  22.        Debug.Print "Report and path: " & strReportFile
  23.        DoCmd.OutputTo objecttype:=acOutputReport, objectname:=strReport, outputformat:=acFormatPDF, outputfile:=strReportFile
  24.      Call .MoveNext
  25.    Loop
  26.  End With
  27.  
  28.      DoCmd.Close acReport, "rptECR"
  29.  
  30. End Sub
Below are links to previous questions that aided in the building of this VBA:
Save to PDF File Name AutoCreate
Print Report for each Filtered Record
Any ideas?

Sgt B
Feb 1 '12 #1

✓ answered by NeoPa

  1. Empty string not necessary in line #15 :
    Expand|Select|Wrap|Line Numbers
    1. strPath = strCurrentPath & "\" & strmonth & " ECR\"
  2. Line #19 not required after all working.
  3. Lines #21 and #30 not required and misleading. Do Until .EOF handles this properly as an empty recordset returns .EOF and .BOF as True. There are some situations where .RecordCount is not set until after this point (or at all in some circumstances).
  4. strPDFName and strFileName are both for the same purpose so one of them should be ditched.
  5. Whichever is ditched line #11 is useless and can go.
  6. Line #23 changes to :
    Expand|Select|Wrap|Line Numbers
    1. (whichever variable is used) = !RANK & " " & !LNAME & ", " & !FNAME.PDF

Otherwise all should be fine. Nice attempt by the way. Much closer to the mark and quite recognisable as to what it's trying to do.

19 2587
TheSmileyCoder
2,322 Expert Mod 2GB
It would have been very helpfull, if you had posted which line gives the error.

Now I am guessing that you are getting the error on line 23, and I am guessing the cause is that strReportFile is Null. The underlying cause for this is lines 12 and 15.
Line 12 and 15:
Expand|Select|Wrap|Line Numbers
  1. strFilter = "[SSN] = ' & !SSN & '" 
  2. strReportFile = DLookup(Expr:="[location]", Domain:="[qrymember]", Criteria:=strFilter) 
  3.  
The problem here is that the dlookup function gets passed a string that makes no sense to it. It is basicly trying to find any record in which the field [SSN] exactly matches the string LITERAL ' & !SSN & '. For more unstanding, try below line 12 to add:
Expand|Select|Wrap|Line Numbers
  1. Msgbox strFilter
I am going to guess that the command is executed from a form in which you have a textbox called SSN. The correct syntax in that case would be:
Expand|Select|Wrap|Line Numbers
  1. strFilter = "[SSN] = '" & Me.SSN & "'" 
Again, try to add a msgbox strFilter below after you have made the modifications to get a greate understanding of what is going on.


Whenever you encounter an error such as this one, the best approach is to try to find out which of your variables are null, and then try to determine why it is null.
Feb 1 '12 #2
NeoPa
32,556 Expert Mod 16PB
Indeed Stevan, as Smiley says, including a line number with the error message is very much easier for others to work with than using highlights such as Bold, or even comments, in the code itself. That said of course, it's another well-asked question.

I'll follow it, but I'm working from my phone ATM so cannot contribute much now (not that I need to I suspect, as Smiley has you covered).
Feb 1 '12 #3
Brilstern
208 100+
OK gentlemen,

I have taken your input into consideration but I am a little lost here. the below code is my newest change to try and accommodate but I am still not sure how to treat one section. Line 15 is where I am getting the Null problem. I do know why but I don't know how to direct it. This code is a mix of an individual PDF creator that saves one report as a PDF and saves the name as the [location] 'a combobox that I use to choose who to select; and a loop that NeoPa helped me work to print an individual report for each SSN in tblBand Members. What I have attempted is to use them together but of course the naming attempt has me in a bind. I don't know how to associate each name with each PDF as they save. Which is where the error is. What is the correction to line 12 to correctly associate the name with?


Expand|Select|Wrap|Line Numbers
  1. Dim cdb As DAO.Database
  2. Dim strFilter As String
  3. Dim strCurrentPath As String
  4. Dim strReportFile As String
  5. Dim strReport As String
  6. Dim strFilter2 As String
  7. Dim strInputFileName As String
  8. Dim strmonth As String
  9. Set cdb = CurrentDb
  10. strFilter = "[SSN] = ' & !SSN & '"
  11. strFilter2 = "[location] = ' & !Location & '"
  12. strmonth = Me.MonthSelect
  13. strCurrentPath = Application.CurrentProject.Path
  14.  MsgBox strFilter2
  15.  strReportFile = DLookup(Expr:="[LOCATION]", Domain:="[qrymember]", Criteria:=strFilter2)
  16.  strReportFile = Replace(strCurrentPath & "\strmonth & ' ECR'\%N.PDF", "%N", strReportFile)
  17. strReport = "rptECR"
  18.  
  19.    With cdb.TableDefs("[tblBand Members]").OpenRecordset(dbOpenTable)
  20.    Do While Not .BOF And Not .EOF
  21.    Call DoCmd.OpenReport(ReportName:="rptECR", View:=Access.AcView.acViewReport, WhereCondition:=strFilter)
  22.    Debug.Print "Report and path: " & strReportFile
  23.    DoCmd.OutputTo objecttype:=acOutputReport, objectname:=strReport, outputformat:=acFormatPDF, outputfile:=strReportFile
  24.    Call .MoveNext
  25.    Loop
  26.    End With
  27.  
  28. DoCmd.Close acReport, "rptECR"
Thx Gents,

Sgt B
Feb 1 '12 #4
NeoPa
32,556 Expert Mod 16PB
When you use a reference to an item that starts with a dot (.) or a bang (!) it must be within code designated by a line starting with a With .

Your lines #10 and #11 seem to be referring to a recordset which hasn't been introduced at that point (See line #19). They also seem to be failing as they assign a simple literal string instead of building a result string from literal strings mixed with a variable. When moved, they should be more like :
Expand|Select|Wrap|Line Numbers
  1. strFilter = "[SSN] = '" & !SSN & "'"
  2. strFilter2 = "[location] = '" & !Location & "'"
Feb 1 '12 #5
Brilstern
208 100+
OK, this is obviously way above my head because I am kinda lost. Would taking out the criteria in the Dlookup LN 15 help. I tried this and I get OutputTo canceled.

Sgt B
Feb 1 '12 #6
NeoPa
32,556 Expert Mod 16PB
It's difficult to explain, as there are really so many misunderstandings and confusions evident in your code.

Let's look at one though, and then I would suggest you reconsider asking your question without reference to your code. In fact, you shouldn't even be posting code that hasn't passed the compile test first (See Before Posting (VBA or SQL) Code).

Your Code :
  1. Expand|Select|Wrap|Line Numbers
    1. strFilter = "[SSN] = ' & !SSN & '"
    2. where !SSN = "ABC" leaves strFilter set to :
    3. [SSN] = ' & !SSN & '
  2. Expand|Select|Wrap|Line Numbers
    1. strReportFile = Replace(strCurrentPath & "\strmonth & ' ECR'\%N.PDF", "%N", strReportFile)
    2. where strCurrentPath = "C:", strmonth = "Feb" and the original of strReportFile = "ABC" leaves strReportFile set to :
    3. C:\strmonth & ' ECR'\ECR.PDF
Fixed Code :
  1. Expand|Select|Wrap|Line Numbers
    1. strFilter = "[SSN] = '" & !SSN & "'"
    2. where !SSN = "ABC" leaves strFilter set to :
    3. [SSN] = 'ABC'
  2. Expand|Select|Wrap|Line Numbers
    1. strReportFile = Replace(strCurrentPath & "\" & strmonth & " ECR\%N.PDF", "%N", strReportFile)
    2. where strCurrentPath = "C:", strmonth = "Feb" and the original of strReportFile = "ABC" leaves strReportFile set to :
    3. C:\Feb ECR\ABC.PDF
Feb 1 '12 #7
Brilstern
208 100+
NeoPa,

When I use
Expand|Select|Wrap|Line Numbers
  1. strFilter = "[SSN] = '" & !SSN & "'"
I get "Invalid or Unqualified Resource" compile error.

But after all of this I have been doing more reading on DLookup and it doesn't seem to be to friendly when trying to loop it.

How does it know what [location] to put on each PDF? I mean how does it all work. I am really having trouble here.
Feb 1 '12 #8
NeoPa
32,556 Expert Mod 16PB
This leads into what I was saying about how wrong your code is. It's full of problems and confusions. This is why I offered the suggestion in the last post to re-present your question a different way, as the code makes a poor basis from which to ask about it.

!SSN makes no sense as :
  1. There is no related With statement.
  2. The recordset it relates to hasn't even been opened yet.

This isn't the only issue with the code, so I expect you can start to see why I made the suggestion. You can continue to pick at individual lines, but it'll be a long journey that way.

PS. Don't let this dishearten you. You still have a lot going for you in as much as you ask a better question than most of our experienced members. Appreciate also, that we frequently get questions from members asking us to unravel code they've managed to put together from multiple sources. It's a lot harder than it appears. Fine if you are already able to put the code together for yourself anyway, but if you need the examples in order to do it, then cobbling the disparate code together is always going to be a struggle.
Feb 1 '12 #9
Brilstern
208 100+
NeoPa,

I am soon realizing that putting this code together is much more difficult than I imagined. This is one of my first attempts on piecing together such a complex code. I don't want to scrap it because I know the code works without the naming function. I took out the dlookup and it works fine but you have to individually name each PDF. Do you have any ideas on a better way to attack this. I appreciate all the help and constructive criticism. I understand your basis and frustration as I have trouble understanding the simple stuff sometimes.

Sgt B
Feb 2 '12 #10
NeoPa
32,556 Expert Mod 16PB
I'm sure I could help, but I have to have a clear framework to start from. I don't remember all your questions in detail (I deal with a number of threads every day), so I need you to specify your requirement clearly in words if you can. I need to know particularly what information is available to start with and what is required as a result.
Feb 2 '12 #11
Brilstern
208 100+
OK, I really appreciate it. I will be in in the morning and and let you know
Feb 2 '12 #12
NeoPa
32,556 Expert Mod 16PB
Just a heads-up - Friday is a busy day for me this week and I may not respond immediately. Weekends often allow me to catch up though. It just means you may have to wait till Monday to try it out.
Feb 2 '12 #13
Brilstern
208 100+
OK so the break down!

I am trying to make a report for each [SSN] in [tblBand Members] and then convert them all to PDF. As they convert to pdf i am trying to name them [Rank LName, Fname]. The source table is below.

Expand|Select|Wrap|Line Numbers
  1.         Field                      Type
  2.         Rank                       Text
  3.         FName                      Text
  4.         LName                      Text
  5.         MI                         Text
  6.         SSN                        Text PK
  7.         MOS                        Text
  8.         EAS                        Date/Time
  9.         Instrument                 Text
  10.         Date Checked In            Date/Time
  11.         Supply Rep                 Text
Sgt B
Feb 3 '12 #14
NeoPa
32,556 Expert Mod 16PB
That seems clear enough Stevan. I'll look into this before Monday and post some code that should do the trick.
Feb 3 '12 #15
Brilstern
208 100+
Got it working with the code below! Yay ME. Ok so I still have one issue though. In LN 23 i have the name of the PDF. Currently it is just the last name of the Marine. I want it to look like LN 11 "RANK & ' ' & LNAME & ', ' & FNAME". Any ideas

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim rs As DAO.Recordset
  3. Dim strSQL As String
  4. Dim strPath As String
  5. Dim strFileName As String
  6. Dim strReport As String
  7. Dim strCurrentPath As String
  8. Dim strmonth As String
  9. Dim strPDFNAme As String
  10.  
  11. strPDFNAme = "RANK & ' ' & LNAME & ', ' & FNAME"
  12. strmonth = Me.MonthSelect
  13. strCurrentPath = Application.CurrentProject.Path
  14. strSQL = "SELECT DISTINCT SSN, RANK, LNAME, FNAME FROM [TBLBAND MEMBERS]"
  15. strPath = "" & strCurrentPath & "\" & strmonth & " ECR\"
  16. strReport = "rptECR"
  17. Set db = CurrentDb()
  18. Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
  19. MsgBox strPath
  20. With rs
  21.    If .RecordCount > 0 Then
  22.       Do Until .EOF
  23.          strFileName = .Fields!LName & ".pdf"
  24.          DoCmd.OpenReport strReport, acViewPreview, , "[SSN] = '" & !SSN & "'", acHidden
  25.          Call DoCmd.OutputTo(objecttype:=acOutputReport, objectname:=strReport, outputformat:=acFormatPDF, outputfile:=strPath & strFileName)
  26.          DoEvents
  27.          DoCmd.Close acReport, strReport
  28.          .MoveNext
  29.       Loop
  30.    End If
  31. End With
  32.  
  33. rs.Close
  34. Set rs = Nothing
  35. Set db = Nothing
  36. DoCmd.Close acForm, "frmPDFSave"
Thank you both so much for the help, i am understanding it a little better now.

Sgt B
Feb 3 '12 #16
NeoPa
32,556 Expert Mod 16PB
  1. Empty string not necessary in line #15 :
    Expand|Select|Wrap|Line Numbers
    1. strPath = strCurrentPath & "\" & strmonth & " ECR\"
  2. Line #19 not required after all working.
  3. Lines #21 and #30 not required and misleading. Do Until .EOF handles this properly as an empty recordset returns .EOF and .BOF as True. There are some situations where .RecordCount is not set until after this point (or at all in some circumstances).
  4. strPDFName and strFileName are both for the same purpose so one of them should be ditched.
  5. Whichever is ditched line #11 is useless and can go.
  6. Line #23 changes to :
    Expand|Select|Wrap|Line Numbers
    1. (whichever variable is used) = !RANK & " " & !LNAME & ", " & !FNAME.PDF

Otherwise all should be fine. Nice attempt by the way. Much closer to the mark and quite recognisable as to what it's trying to do.
Feb 3 '12 #17
Brilstern
208 100+
Ok, when I do this it gives me error "Invalid or Unqualified Resource" compile error on LN 23. It highlights !Rank

Sgt B
Feb 3 '12 #18
Brilstern
208 100+
NeverMind. I forgot to re add a folder in the path that I created this afternoon. simple mistake. Everything is all good!!
Feb 3 '12 #19
NeoPa
32,556 Expert Mod 16PB
Stevan Bias:
Everything is all good!!
That's what I like to hear! Good for you :-)
Feb 3 '12 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sigurd Bruteig | last post by:
Hi all! I have a problem printing multiple reports. The code i use is: Dim stDocName As String stDocName = "rptInvoice" DoCmd.OpenReport stDocName, acNormal, , " = date()" The problem is that...
3
by: MHenry | last post by:
Hi, I have 30 separate Access reports to print to pdf files. Presently, I print these reports to Acrobat pdf files one report at a time. I am looking for some help, or a program or add-in...
0
by: mr_doles | last post by:
I have finally got the hang of this ReportViewer control. The one question that I have is dealing with multiple reports. If I have, lets say, 10 rdlc reports should I have 10 WinForms to put them...
2
by: Thall | last post by:
Hey Gurus - I've seen a few solutions to this problem, but none of which I can do without a little help. Here's the situation The following code loops thru a sales report, using the sales rep ID...
2
by: jonvan20 | last post by:
I have been having trouble with a simple Dlookup command that was Reccommended to me by a nice fellow named Vic, On the other hand I have statements like this that wont run they give me a run time...
3
by: joelpollock | last post by:
I'm having trouble continuously page numbering a large report in MS Access. The report is made up of three separate Access reports which I join together at the end. In the past I have opened the...
4
by: Mark1978 | last post by:
Hi All Apologies if someone has asked this question before, but I have hunted high and low for the solution. I am relatively new to using access so am not sure if this is possible. I am using...
3
reginaldmerritt
by: reginaldmerritt | last post by:
I'm using Dlookup to search a table for the first record it comes across that has a date >= the date i specifiy. e.g. formateddate = format(Me.SelectedDate,"mmddyy") VarX = DLookup("",...
3
by: evenlater | last post by:
I have an Access application on a terminal server. Sometimes my users need to export reports to pdf, rtf or xls files and save them to their own client device hard drives. They can do that right...
0
by: mgarg005SSRS | last post by:
My requirement is to create multiple reports (.rdlc) and show them using single report viewer control. A: User selects a report from drop down list. B: Depending on report name a report has to be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.