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. -
Dim pass as String
-
Dim text1 as String, text2 as String
-
-
text1 = me.textbox1
-
text2 = me.textbox2
-
pass = generatecode(text1, text2)
-
debug.print pass
-
Me.regNo = pass
-
debug.print Me.regNo
-
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
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
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.
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
Is the Return Value of the generatecode() Function a String, explicitly stated, as in: - Public Function generatecode(strArg1, strArg2) As String
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 -
Dim strString As String
-
strString = "0001234"
-
Text4 = strString
-
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.
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.
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?
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.
@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.
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.
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: - Dim pass As String
-
-
'Simulating the Return Value from generatecode(Text1, Text2)
-
pass = "0012345"
-
-
Me![txtTest] = Format$(pass, String$(Len(pass), "0"))
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.
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.
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.
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: - Dim pass as String
-
Dim text1 as String, text2 as String
-
-
text1 = me.textbox1
-
text2 = me.textbox2
-
pass = generatecode(text1, text2)
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
|
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...
| |