|
-
May 21st, 2013, 04:07 AM
#1
Thread Starter
Lively Member
[RESOLVED] need help checking string is formatted correctly as mac address
hey guys
i have a listbox that imports a text file full of mac address's like so
00:00:00:00:00:00
ff:ff:ff:00:ff:ff
the issue is when someone imports the list like this
000000000000 blah blah
ffffffffffff blah blah
ff:ff:ff:ff:ff:ff blah blah
id there an easy way to recognise this issue and rectify it whilst loading each line into the textbox
-
May 21st, 2013, 05:24 AM
#2
Re: need help checking string is formatted correctly as mac address
 Originally Posted by gerble1000
id there an easy way to recognise this issue and rectify it whilst loading each line into the textbox 
Yes. You'll probably like the Like operator:
Code:
Const MAC_ADDR_FORMAT = "[0-9A-F][0-9A-F]:" & _
"[0-9A-F][0-9A-F]:" & _
"[0-9A-F][0-9A-F]:" & _
"[0-9A-F][0-9A-F]:" & _
"[0-9A-F][0-9A-F]:" & _
"[0-9A-F][0-9A-F]"
If UCase$("01:23:45:67:89:AB") Like MAC_ADDR_FORMAT Then
'Input string matched MAC address format
Else
'Input string didn't matched MAC address format
End If
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
May 22nd, 2013, 04:55 AM
#3
Thread Starter
Lively Member
Re: need help checking string is formatted correctly as mac address
im liking this method but i cannot get it to work
here i my code
Const MAC_ADDR_FORMAT = "[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]"
open filename for input as #1
do while not eof(1)
line input #1, variable8
variable8 = left(variable8, 17)
if (variable8) like MAC_ADDR_FORMAT then
list2.additem variable8
else
msgbox(variable8 & " is the incorrect format")
exit do
end if
loop
close#1
i taken the UCase out as it does not matter about it being upper or lower case.
can you see where i have gone wrong
i just get the msgbox popup with the correct mac address format
also the mac address's i am using is all uppercase
-
May 22nd, 2013, 05:53 AM
#4
Re: need help checking string is formatted correctly as mac address
Can you post a sample of the text file? The UCase$ function may be left out if you can be absolutely sure that the MAC addresses will always be uppercase.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
May 22nd, 2013, 06:11 AM
#5
Thread Starter
Lively Member
Re: need help checking string is formatted correctly as mac address
 Originally Posted by Bonnie West
Can you post a sample of the text file? The UCase$ function may be left out if you can be absolutely sure that the MAC addresses will always be uppercase.
sure
00:11:E6:99:41:A7 01/05/2013
00:44:FE:FE:A1:22 02/03/2013
pretty much like that
the list may also have lowercase but i would allow lowercase to go through so does that mean i have to change the MAC_ADDR_FORMAT to [0-9a-fA-F]
-
May 22nd, 2013, 06:58 AM
#6
Re: need help checking string is formatted correctly as mac address
 Originally Posted by gerble1000
sure
00:11:E6:99:41:A7 01/05/2013
00:44:FE:FE:A1:22 02/03/2013
pretty much like that
Your code and sample text works fine for me. Have you tried stepping through your code and verifying that you're getting the expected text from the file?
 Originally Posted by gerble1000
the list may also have lowercase but i would allow lowercase to go through so does that mean i have to change the MAC_ADDR_FORMAT to [0-9a-fA-F]
You can do it that way or you can instruct the Like operator to use case-insensitive comparisons by specifying Option Compare Text.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
May 22nd, 2013, 09:44 AM
#7
Thread Starter
Lively Member
Re: need help checking string is formatted correctly as mac address
my bad
i had
if (vairable8)
when it should of been
if (variable)
-
May 22nd, 2013, 10:04 AM
#8
Re: need help checking string is formatted correctly as mac address
Hmm, it looks like you don't have Option Explicit enabled. Now you know what happens when you don't use it. 
If you have no more issues, you may now mark this thread Resolved!
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|