Visual Basic Help

minendo

Elite Member
Aug 31, 2001
35,560
22
81
I've got a text box that requires a 16 digit number be inputted as an account number. From that I need to split up the number into four different numbers and store them in an access database. I can't remember how to take only parts of the string. What I need is a setup like this:

First Entry: First 3 numbers of account number
Second Entry: Numbers 4, 5, 6, 7 of account number
Third Entry: Numbers 8, 9, 10, 11 of account number
Fourth Entry: Numbers 12, 13, 14, 15, 16 of account number.

I've got the code all configured to store the numbers into the database, but I can not remember the string modification code. Any ideas?


Will this work? 'If txtAccountNumber.Text <> "" Then fRS("Fund") = Left(txtAccountNumber, 3)
 

SaltBoy

Diamond Member
Aug 13, 2001
8,975
11
81
Are you talking about the mid function?

Dim sText As String
Dim sMidText As String

sText = "123456789"
sMidText = Mid$(sText, 3, 6)

sMidText = "3456789"

I believe that's it. It could be "345678" though. It's been a while.
 

AndyHui

Administrator Emeritus<br>Elite Member<br>AT FAQ M
Oct 9, 1999
13,141
17
81
left(string, number of characters), substr(string, offset, number of characters), right(string, number of characters).
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
'If txtAccountNumber.Text <> "" Then fRS("Fund") = Left(txtAccountNumber, 3)
'If txtAccountNumber.Text <> "" Then fRS("Dept") = substr(txtAccountNumber, 4, 4)
'If txtAccountNumber.Text <> "" Then fRS("Project") = substr(txtAccountNumber, 8, 4)
'If txtAccountNumber.Text <> "" Then fRS("Dref") = Right(txtAccountNumber, 5)


There is what I have done, does it look right? Shoud I use and instead of the 4 If statements?
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: minendo
'If txtAccountNumber.Text <> "" Then fRS("Fund") = Left(txtAccountNumber, 3)
'If txtAccountNumber.Text <> "" Then fRS("Dept") = substr(txtAccountNumber, 4, 4)
'If txtAccountNumber.Text <> "" Then fRS("Project") = substr(txtAccountNumber, 8, 4)
'If txtAccountNumber.Text <> "" Then fRS("Dref") = Right(txtAccountNumber, 5)


There is what I have done, does it look right? Shoud I use and instead of the 4 If statements?

Personally, I'd use a regular expression:

(\d{3})(\d{4})(\d{4})(\d{5})

You could use this w/ the RegExp object. If you feel comfortable doing that, let me know and I'll give you an example.

Your code is syntactically incorrect, and you're duplicating code. I would rewrite as follows:

If txtAccountNumber.Text <> vbNullString Then
fRS("Fund") = Left(txtAccountNumber, 3)
fRS("Dept") = Mid(txtAccountNumber, 4, 4)
fRS("Project") = Mid(txtAccountNumber, 8, 4)
fRS("Dref") = Right(txtAccountNumber, 5)
End If
 

tkdkid

Senior member
Oct 13, 2000
956
0
0
Originally posted by: minendo
'If txtAccountNumber.Text <> "" Then fRS("Fund") = Left(txtAccountNumber, 3)
'If txtAccountNumber.Text <> "" Then fRS("Dept") = substr(txtAccountNumber, 4, 4)
'If txtAccountNumber.Text <> "" Then fRS("Project") = substr(txtAccountNumber, 8, 4)
'If txtAccountNumber.Text <> "" Then fRS("Dref") = Right(txtAccountNumber, 5)


There is what I have done, does it look right? Shoud I use and instead of the 4 If statements?

Hmm....wouldn't this be better:
If len(txtAccountNumber.Text) = 16 then
fRS("Fund") = Left(txtAccountNumber, 3)
fRS("Dept") = substr(txtAccountNumber, 4, 4)
fRS("Project") = substr(txtAccountNumber, 8, 4)
fRS("Dref") = Right(txtAccountNumber, 5)
else
msgbox "You = tHe Stoopid!! Enter a valid account number."
txtaccountnumber.setfocus
end if
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
Originally posted by: tkdkid
Hmm....wouldn't this be better:
If len(txtAccountNumber.Text) = 16 then
fRS("Fund") = Left(txtAccountNumber, 3)
fRS("Dept") = substr(txtAccountNumber, 4, 4)
fRS("Project") = substr(txtAccountNumber, 8, 4)
fRS("Dref") = Right(txtAccountNumber, 5)
else
msgbox "You = tHe Stoopid!! Enter a valid account number."
txtaccountnumber.setfocus
end if
I have already set up the validation statements elswhere in the save command. I am editting a University Funding/Purchasing Program that was written several years ago and don't want to be too different from what they originally did. If it were up to me I would redo the entire program, but all they want right now is the account number to be placed in the purchasing section of the program.

 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
Originally posted by: ffmcobalt
Pssssst... try posting in the correct forum ;)
Psst. So I make two threads in the wrong forum both within the past two days. I hope the mods dont ban me or yell at me.
 

dullard

Elite Member
May 21, 2001
26,066
4,712
126
Originally posted by: ffmcobalt
Pssssst... try posting in the correct forum ;)

I've been posting visual basic questions here for well over a year, and only once did anyone ever think it wasn't Off Topic. I got my questions answered quickly and the answers were useful. Then I posted it in software a few times and never, ever got any responses. So Off Topic is definately the best place to post these questions.
 

NikPreviousAcct

No Lifer
Aug 15, 2000
52,763
1
0
Originally posted by: dullard
Originally posted by: ffmcobalt
Pssssst... try posting in the correct forum ;)

I've been posting visual basic questions here for well over a year, and only once did anyone ever think it wasn't Off Topic. I got my questions answered quickly and the answers were useful. Then I posted it in software a few times and never, ever got any responses. So Off Topic is definately the best place to post these questions.

And who gives a small rodent's sphincter? Not me. There's a forum for software. Post software questions there. Off Topic is not for Software questions, and that's that. If you wonder why you don't get software answers here, perhaps you need to realize that you're posting on a forum of a site dedicated to hardware. Granted, these forums aren't limited to hardware, but if you have a car question, you don't go to the baker. Either post in the software forum or find another software forum to ask on if you don't get your questions answered. Really. Off Topic gets topics like these moved way too often.
 

DT4K

Diamond Member
Jan 21, 2002
6,944
3
81
try www.tek-tips.com
I've had much better luck getting help with programming questions there. Any time I post there, I get good help. Usually within hours. Anandtech is much more of a hardware and general computer problem site.