Solved Can I run FORMAT function using VBA? (1 Viewer)

iCyrius

New member
Local time
Today, 05:50
Joined
Apr 5, 2016
Messages
18
Hello. My code reads a number from a table and needs to be converted to string. In Excel's VBA you can use application.worksheetFunction to trigger any Excel Formula, but I cannot find that same type of code in Access.

My line would look somethin like this,

StrIDLocator = FORMAT(me.Number, "000000")

Thanks
 

Minty

AWF VIP
Local time
Today, 13:50
Joined
Jul 26, 2013
Messages
10,371
I would always advise against converting a number to a string unless it is for display purposes.
Your code looks fine assuming this is to display leading zero's e.g.
100 > 000100
2063 > 002063
etc. etc.
 

plog

Banishment Pending
Local time
Today, 07:50
Joined
May 11, 2011
Messages
11,643
Works for me:

Code:
Private Sub test()

    Dim i As Integer
    Dim s As String
   
    i = 7
    s = Format(i, "000000")
    MsgBox (s)

End Sub

When I run the above it shows me a dialog with 000007

Can you post more of your code? Are you sure Me.Number contains data?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:50
Joined
Oct 29, 2018
Messages
21,467
Hi. Welcome to AWF!

What happens if you tried that formula in Access?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:50
Joined
May 7, 2009
Messages
19,230
you need to create a Form for you table.
add code to AfterUpdate event of Number field:

Private Sub Number_AfterUpdate()
StrIDLocator = FORMAT(me.Number, "000000")
End sub
 

iCyrius

New member
Local time
Today, 05:50
Joined
Apr 5, 2016
Messages
18
Thank you for your help. It did not occur to me to write it like I posted it. I was trying something like,

StrIDLocator = application.function("FORMAT(me.Number,"0000")

It works now. Perfect!
 

CJ_London

Super Moderator
Staff member
Local time
Today, 13:50
Joined
Feb 19, 2013
Messages
16,607
You can also use the format property for a field or control if you want to retain the underlying numeric value
 

Users who are viewing this thread

Top Bottom