Add Leading 0's to FileName Based on Variable Length

hamidxa

New Member
Joined
Oct 11, 2013
Messages
19
I have a piece of code that is used to generate PDF files.
The problem however is that the file names need a 3 digit prefix, based on the variable District.

So for example, it will generate a file named:
1_PM.pdf
Whereas I need it to generate a file named:
001_PM.pdf

This is not a problem for values 100-999, as they by default will have a 3-dig number prefix, but for numbers 1-99, this is definitely an issue.

Not sure how to code for this, but here is what I have presently...thanks in advance for any help! :

Code:
[/COLOR][COLOR=#333333]Private Sub Command82_Click() 'Mercator[/COLOR]
[COLOR=#333333]Dim rst As DAO.Recordset[/COLOR]
[COLOR=#333333]Dim strSQL As String[/COLOR]
[COLOR=#333333]Dim strPath As String[/COLOR]

[COLOR=#333333]strSQL = "SELECT [qryDISTRIBUTION_DMList].District FROM [qryDISTRIBUTION_DMList]"[/COLOR]

[COLOR=#333333]Set rst = CurrentDb.OpenRecordset(strSQL)[/COLOR]

[COLOR=#333333]strTo = ""[/COLOR]
[COLOR=#333333]strCC = ""[/COLOR]
[COLOR=#333333]strBCC = ""[/COLOR]
[COLOR=#333333]strFrom = ""[/COLOR]
[COLOR=#333333]strSubject = ""[/COLOR]
[COLOR=#333333]strBody = ""[/COLOR]

[COLOR=#333333]strPath = Me.tbxFolderLocation1[/COLOR]

[COLOR=#333333]With rst[/COLOR]
[COLOR=#333333]If Not .EOF And Not .BOF Then[/COLOR]
[COLOR=#333333].MoveFirst[/COLOR]
[COLOR=#333333]Do While Not .EOF[/COLOR]
[COLOR=#333333]strGlobalWhere = "District=" & !District[/COLOR]

[B][COLOR=#333333]DoCmd.OutputTo acOutputReport, "rptDistrict_Combine_Mercator", acFormatPDF, strPath & "SOPM." & !District & "_PM" & ".pdf", False[/COLOR][/B]

[COLOR=#333333].MoveNext[/COLOR]
[COLOR=#333333]Loop[/COLOR]
[COLOR=#333333]Else[/COLOR]
[COLOR=#333333]MsgBox "No Region records found", vbOKOnly[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333].Close[/COLOR]
[COLOR=#333333]End With[/COLOR]

[COLOR=#333333]Set rst = Nothing[/COLOR]
[COLOR=#333333]MsgBox "Done!"[/COLOR]

[COLOR=#333333]End Sub

[/COLOR][COLOR=#333333]
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You can format any numeric variable to exactly three digits with leading zeroes like this:
FORMAT(VariableName,"000")
 
Upvote 0
Joe,
Thanks for that tip!

I will give it a shot asap, but just to be clear, would this work:

DoCmd.OutputTo acOutputReport, "rptDistrict_Combine_Mercator", acFormatPDF, strPath & FORMAT(!District,"000") & "_PM" & ".pdf", False
 
Last edited:
Upvote 0
That's fine. Except the SOPM. part of the string concatenation from the original code example is no longer there (whether that was intentional or not I don't know).
 
Upvote 0
Sorry for any confusion, but yes, it would need to be stripped out.
Just wanted to confirm that this worked, thanks so much again for your support!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top