Help needed.....(file IO, format function)

Sadie

Member
Joined
Oct 31, 2005
Messages
7
Location
Cambridge
Programming Experience
1-3
I have created a form that has 5 text boxes and a button. When the user enters a value in the boxes they click on the button to save the data.

The saved data must then be read to a text file in a required format.
For example, the data entered in textbox1 is a reference number. The reference number must be 6 digits. Therefore, when the data is read to the file the reference number will show 6 digits.

Does anyone know how to use the format function to ensure that 6 digits are read to the text file? :confused:
 
VB.NET:
[SIZE=2]textbox1.Text = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TextBox1.Text.PadLeft(6, "0")
[/SIZE]

Change the "0" to whatever char you want to fill the extra spaces with.

Use it in an if/then/else. Check with .Length to make sure it's not over 6 and then pad it.
 
Last edited:
[Resolved] Help needed...(file IO, format function)

Hi sevenhalo, thanks for your help! I have tried the code and it works perfectly. :D

Here is my code:

VB.NET:
If Len(txtRefNumber.Text) < 6 Then
txtRefNumber.Text = Me.txtRefNumber.Text.PadLeft(6, "0")
End If

Thanks again!!
 
Actually, you may want to do something like this:

VB.NET:
If txtRefNumber.Text.Length > 6 Then
  Messagebox.show("Input not valid")
else
  txtRefNumber.Text = Me.txtRefNumber.Text.PadLeft(6, "0")
End If

The Pad functions check to make sure it's within the bounds; you don't need to.

Your Welcome. :)
 
Back
Top