Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formating a text box

Status
Not open for further replies.

Chummly1984

Technical User
Sep 8, 2007
11
US
Have a quick question.

Here is a code I am using to convert decimal serial numbers into Hex.

Private Sub txtESNdec_AfterUpdate()
left3 = Left([txtESNdec], 3)
right8 = Right([txtESNdec], 8)
hexleft = Hex(left3)
hexright = Hex(right8)
hex_number = hexleft & Right("000000" & hexright,6)

Me.[txtESNhex] = hex_number
Me.txtDateCode.SetFocus

The problem I am having is that on some calculations, the txtESNhex number starts with a 0 (Zero) and its dropping that zero. I need to have that stay, so instead of getting something like B7E12A0, it needs to say 0B7E12A0

I tried the input mask, but that didnt help. I made these text boxes since these ESN's do contain both numbers and letters.

Any help would be much appreciated!!
 
Additional info. Not all conversions start with 0, some start with 1 or 2, etc... Maybe it would be best to check the length of the result, and if less than 8 characters, to add the "0" in front??

thanks again all!
 
Well, format(AnyNumber,"000000") will always return AnyNumber with enough leading zeros so that t's 6 digits long.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Sorry, meant for that to be Format(AnyNumber,"00000000")

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
I tried, but it doesn't seem to work. Remember these are text boxes, not numeric boxes.
 
well, I got around it by saying:

If Len(hex_number) = 7 Then
newhex = "0" & hex_number
Else
nexhex = hex_number
End If

Sometimes I think better in the morning. Thanks for the help!!!
 
I'm glad you got it working for you, but exactly what is a "numeric box?" Data in forms, whether numeric or text, is entered in text boxes! The code I gave you does the job you've set out, whether the variable AnyNumber is an actual number or text made up of digits. It does in 1 line instead of in 5 lines, as your does, and has the added advantage of being usable in a query calculation.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Hey Missingling,

What I ment was that it is a text box, which normally can have a 8 digit number, or a combination of numbers and characters. I guess what I ment was that it is not always just a "number" in that box. I'm still a rookie when it comes to this stuff, so I may not explain clearly what I am trying to say. Sorry about that.

Maybe I just didn't put your code suggestion in the right place. I'll try playing around with it. Heck, one line of code is easier than 5, like you said :)
 
Missinglinq

I tried the format method in VBA and it returned the following..

Debug.Print Format(Hex(2), "000000")
result: 000002
Debug.Print Format(Hex(20), "000000")
result: 000014
Debug.Print Format(Hex(200), "000000")
result: C8
Debug.Print Format(Hex(2000), "000000")
result: 000007
Debug.Print Format(Hex(20000), "000000")
result: 400000000000000000000

So Chummly was correct in saying this did not work.
I must say, I am surprised at the results too.

Ian M (UK)


Ian Mayor (UK)
Program Error
Always make your words sweet and nice. Because you never know when you may have to eat them.
 
How are ya Chummly1984 . . .

Curious . . . since leading zero(s) have no significance in the actual hex value, why is this so important to you?

I can understand an issue with formatting but here too were talking different lengths of values with leading zero. I see this making things harder to read.

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
Code:
Public Function testFormat(theHex As Variant) As String
  testFormat = Replace(Format(Hex(theHex), "@@@@@@@@"), " ", "0")
End Function
output for 2,20,200,2000,2000 (assuming this is what you want with leading 0):

00000002
00000014
000000C8
000007D0
00004E20

This function could be used in a query.

Do not know why, but when you try to format the string 4E20 it assumes that this is scientific notation and converts it first to 4000000000000000000000.
 
Hey Aceman,

I know that a leading zero is kind of irreliant, but in my line of work, its required. the number submitted on a form has to be 8 digits and if it starts with a zero, it has to be there. If the ;eading zero isnt there, then the first number or character on the number that actually shows would actually be considered the 1st digit and when you convert that back to decimal, you get an entirely different number.

Richard
 
Hey MajP,

Thanks for the input. thanks to all for the assist. Thats what I love about this site, everyone is always willing to lend a helping hand!!

Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top