473,394 Members | 2,100 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,394 software developers and data experts.

How to preserve possible leading zeros in a number?

21
Is there a way to preserve possible leading zeros in a number even when it is passed as text?
I have this code on a form which runs off a command button taking two values from user input, passing them to a function and is placing the returned value as a string back in a thrid textbox ('regNo').
The function 'generatecode' is correctly returning a string of digits which may be between 6 to 11 digits long and might contain between 0 and 3 leading zeros. The problem occurs at line 8 when I try to place this string in the text box.
Expand|Select|Wrap|Line Numbers
  1. Dim pass as String
  2. Dim text1 as String, text2 as String
  3.  
  4. text1 = me.textbox1
  5. text2 = me.textbox2
  6. pass = generatecode(text1, text2)
  7. debug.print pass
  8. Me.regNo = pass
  9. debug.print Me.regNo
  10.  
for example the first debug.print is returning 0045678, whilst the second is returning 45678.
It all works fine if there are no leading zeros in the number (but returned as a string) that the function calculates.

Any help gratefully received.
Deekay
Feb 5 '11 #1

✓ answered by Lysander

If push comes to shove and you can't fix it anyother way, create a dummy table and bind RegNo to a text field in that table. btw, what version of Access are you using?

15 9942
Lysander
344 Expert 100+
What is regNo? You say it is a textbox on the form but what type of field is it bound to, a text field or a numeric field. If a text field, it should work fine.

If regNo is unbound, try forcing it to a string using

Me.reNo=chr(34) & pass & chr(34)

and see if that works.
Feb 5 '11 #2
Deekay
21
Hello Lysander
Good try but this is generating an error - and yes the textbox is unbound. It's meant to generate a code for sending as part of an e-mail.
Deekay
Feb 5 '11 #3
ADezii
8,834 Expert 8TB
Is the Return Value of the generatecode() Function a String, explicitly stated, as in:
Expand|Select|Wrap|Line Numbers
  1. Public Function generatecode(strArg1, strArg2) As String
Feb 5 '11 #4
Lysander
344 Expert 100+
Ok, using Access 2003, I have just created a simple form, added an unbound textbox to it and a command button.

The command button has the following code
Expand|Select|Wrap|Line Numbers
  1. Dim strString As String
  2. strString = "0001234"
  3. Text4 = strString
  4.  
And when i hit the command button Text4 displays 0001234

Have you put any formating or anything on the textbox RegNo, because I used a bog-standard default textbox and it worked.
Feb 5 '11 #5
Deekay
21
I haven't used any formatting on the text box - somehow it assumes that the value it is being given is a number and trimming any leading zeros. (Incidentally I also tried temporarily bidning it to a text field and it then behaves properly, so the problem appears to be happening when the textbox is unbound.
I will also try your code to see what happens. It it words I can then compare all of the properties.
Feb 5 '11 #6
Lysander
344 Expert 100+
If push comes to shove and you can't fix it anyother way, create a dummy table and bind RegNo to a text field in that table. btw, what version of Access are you using?
Feb 5 '11 #7
Lysander
344 Expert 100+
Another idea. pass works, so, you could set an interger to the value of len(pass) and then use something like

RegNo=format(pass,"0000000") where the number of zeros are the value of len(pass)

Bit of a cludge but it should work.

Still not sure why you are loseing leading zeros in the first place. I am running 2 commercial databases where the primary keys are all 001, 002, 003 etc and never lose the leading zeros.
Feb 5 '11 #8
ADezii
8,834 Expert 8TB
@Deekay - honestly, I cannot get this NOT to work. Any chance you can Upload the Database, or a portion of it, for us to see? In this manner we can see first hand what the problem is.
Feb 5 '11 #9
Deekay
21
Thabnks for the various suggestions. As you say there is a workaroubnd and I have decided to adopt a similar solution. I have discovered there is no problem if the value is passed to a bound textbox - the leading zeros only get stripped when passing to an unbound textbox. Also I can't use the len(...) idea since there is not way of telling what length (i.e. number of digits) is going to be returned. However the idea of binding it to a dummy filed appears to work, so I will use this.
It does seem stange thought, that VBA doesn't have a function that forces this string to be passed excatly as is. I tried CStr(... and Str(... and neither solved the problem.
As I have a solution i will close this thread now. Thanks for your time.
Feb 5 '11 #10
ADezii
8,834 Expert 8TB
Not sure it you are still initerested or not, but you can try explicitly Formatting the Return Value of the Function, then pass it to a Text Box, as in:
Expand|Select|Wrap|Line Numbers
  1. Dim pass As String
  2.  
  3. 'Simulating the Return Value from generatecode(Text1, Text2)
  4. pass = "0012345"
  5.  
  6. Me![txtTest] = Format$(pass, String$(Len(pass), "0"))
Feb 5 '11 #11
The statement

"regNo = format(CLng(pass),"0000000")"

will change the string value to a long integer value, with the leading 0's.

But in that "regNo" is already showing the correct number, simply setting the field's "Format" property to "0000000" will restore the leading 0's.
Feb 5 '11 #12
ADezii
8,834 Expert 8TB
The function 'generatecode' is correctly returning a string of digits which may be between 6 to 11 digits long and might contain between 0 and 3 leading zeros.
@David Blackman - That will not work since the Return Value from generatecode() can be anywhere from 6 to 11 Digits, that's why the Length of the Return Value needs to be evaluated and the proper Format String generated.
Feb 5 '11 #13
NeoPa
32,556 Expert Mod 16PB
Deekay, I think an important point in all of this is that while String variables will always hold data as strings, TextBox controls on a form are more like Variant variables in that they determine the type of the sata on the fly, depending on their current contents. If you want their values to be specifically strings, than I suggest you pass them into string variables before passing those as parameters to your generatecode() procedure.
Feb 6 '11 #14
ADezii
8,834 Expert 8TB
If you want their values to be specifically strings, than I suggest you pass them into string variables before passing those as parameters to your generatecode() procedure.
Isn't this exactly what happened? The variable were Declared as Strings, set to the Values of Text1 and Text2, then placed as Arguments to the generatecode() Function. Even the Return Value is Declared as String.
From Post #1:
Expand|Select|Wrap|Line Numbers
  1. Dim pass as String 
  2. Dim text1 as String, text2 as String 
  3.  
  4. text1 = me.textbox1 
  5. text2 = me.textbox2 
  6. pass = generatecode(text1, text2) 
Feb 6 '11 #15
NeoPa
32,556 Expert Mod 16PB
Indeed, but the problem isn't noticed in the value of pass, which is displayed in line #7, but only in Me.regNo, which is displayed in line #9 and which is a control.
Feb 7 '11 #16

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

Similar topics

2
by: r.magdeburg | last post by:
//please tell me why... //and give me a hint to solve the problem with leading zeros. //snippet #include <iostream.h> #include <conio.h> int main() { int zahl = 0; cout << "Give me an int...
5
by: samik_tanik | last post by:
I need to export a datagrid to Excel. I could did this. But, also need to keep the leading zeros in the data. How can I acheive this? Any help would be appreciated. -- Thanking you in...
1
by: mmmgood1 | last post by:
Help, I'm linking an excel spreadsheet in access and I have datafields with leading zeros (01021). When the file is linked in access, I get a #num in the field with the leading zeros. The zeros...
6
by: Clint Stowers | last post by:
Using A2k Exporting a query to a CSV file. The problem is any text fields (i.e. 000345) lose any leading zeros. Exporting to an excel file this problem does not exist. Tried to create a...
1
by: Andrew | last post by:
I run a fixed-width export based on a query, and the query looks like this... Clients.SSN, Clients.ClientID, ServiceEvents.SeviceDate, CStr(Format(,"0000")) AS Servs, "28" AS SETTING, FROM...
5
by: OneDay | last post by:
I've got a field that has some old data with text in it, but all forward data will be a 3 digit number. But many of the numbers are still only 2 digits. I would like to force the leading zero in...
6
by: Rich Raffenetti | last post by:
How can one format an integer into a hex string with leading zeros? Suppose an integer is 512 which in Hex is 200. I wish to print the 4-byte integer as 0200 or even 0x0200. The HEX function...
2
by: chris | last post by:
Hi, I have a simple ms access application that allows you to scan barcodes in to a form which stores them in the database. The barcodes are 6 digits in length e.g. 555666 but my handheld...
6
by: JimmyKoolPantz | last post by:
Task: Customer wants a script of the data that was processed in a "CSV" file. Problem: Zip-Code leading zeros are dropped Basically we have a client that has requested a custom script for...
0
by: Monty | last post by:
Hi All, I am having a problem with leading zeros being stripped from fields in a CSV file when I bring them in using Jet/OleDB. In VB.Net/VS 2008, I am accessing a CSV file like so: sSQL =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.