 |
| VB How-To Ask your "How do I do this with VB?" questions in this forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 6th, 2005, 04:00 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
I just told you :)
strUsername = Mid(TextLine$, Instr(TextLine$,"=") + 2)
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
|

January 6th, 2005, 04:01 PM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Im sorry but i dont understand :( Apologies.
Rhys
|
|

January 6th, 2005, 05:20 PM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Right this is whati have :
A BAT file called Test.BAT (with extension)
A form with a label called Label1 and a command button called command1.
This is all of the code that i have :
Code:
Private Sub Command2_Click()
Dim TextLine$, Filename$
Dim FileHandle As Integer
Dim strUserName As String
Filename$ = "Test.BAT"
' Test if the file exists
If Dir(Filename$) = "" Then Exit Sub
FileHandle = FreeFile ' This is safer than assigning a number
Open Filename$ For Input As #FileHandle
Do While Not EOF(FileHandle) ' Loop until end of file
Line Input #FileHandle, TextLine$ ' Read line into variable
' Your code here
strUserName = Mid(TextLine$, InStr(TextLine$, "=") + 2)
Loop
Close #FileHandle
Label1.Caption = strUserName
End Sub
>>>>>>>>>>>>>>>>>>>>>>>>>>>
Private Sub Form_Load()
Open App.Path & "\Test.BAT" For Output As #1
Print #1, "strUserName = rhys"
Print #1, ""
Print #1, ""
Close #1
End Sub
>>>>>>>> indicates a end of the code .. and start of another bit of code.
Now on pressing the command button (note that this needs to be compilied to an exe before it will write to the bat file) the caption of the label should be set to the value that has been taken from the BAT file (rhys)!! But its not, its set to nothing, emtpy, zero, zilch!
I still dont see why i have to put "Dim strUserName as String" in the code because im taking the value from the BAT file?? and not the application itself.
Regards
Rhys
|
|

January 6th, 2005, 06:32 PM
|
|
Authorized User
|
|
Join Date: Dec 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Rhysduk,
strUserName is an arbitrary memory variable used to hold a piece of the string read from the file. It could be called strPiece for that matter. It doesn't necessarily correspond to the text you have written to the file. The caption property of your label needs to be set to a character string.
Dude
|
|

January 7th, 2005, 08:46 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
Here, this may help.
Code:
Private Sub Command2_Click()
Dim TextLine$, Filename$
Dim FileHandle As Integer
Dim strUserName As String
Filename$ = "Test.BAT"
' Test if the file exists
If Dir(Filename$) = "" Then Exit Sub
FileHandle = FreeFile ' This is safer than assigning a number
Open Filename$ For Input As #FileHandle
' Initialize the user in case it's not found in the BAT file
strUserName = "Not Found"
Do While Not EOF(FileHandle) ' Loop until end of file
Line Input #FileHandle, TextLine$ ' Read line into variable
If Instr(Textline$,"=") > 0 Then
strUserName = Mid(TextLine$, InStr(TextLine$, "=") + 2)
End If
Loop
Close #FileHandle
Me.Label1.Caption = strUserName
End Sub
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
|

January 7th, 2005, 08:57 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you very much greg. it is working exactly how i want!
Do i need to repeat the process to get another value for another string in the BAT file?
If not.. what?
Thanks
Rhys
|
|

January 7th, 2005, 09:02 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
Quote:
quote:Originally posted by rhysduk
Do i need to repeat the process to get another value for another string in the BAT file?
|
You're welcome. If you want to get another string out of the BAT file, yes, you need to look for it in a similar way you did the user ID. Fortunately, you have loop already going in this code so you don't have to repeat the whole subroutine again, just put your second search after the first search inside the existing loop.
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
|

January 7th, 2005, 09:06 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I modifies your code that you agve me to this:
Code:
Private Sub Command2_Click()
Dim TextLine$, Filename$
Dim FileHandle As Integer
Dim strUserName As String
Dim strUserReg As Integer
Filename$ = "Test.BAT"
' Test if the file exists
If Dir(Filename$) = "" Then Exit Sub
FileHandle = FreeFile ' This is safer than assigning a number
Open Filename$ For Input As #FileHandle
' Initialize the user in case it's not found in the BAT file
strUserName = "Not Found"
Do While Not EOF(FileHandle) ' Loop until end of file
Line Input #FileHandle, TextLine$ ' Read line into variable
If InStr(TextLine$, "=") > 0 Then
strUserName = Mid(TextLine$, InStr(TextLine$, "=") + 2)
Do While Not EOF(FileHandle) ' Loop until end of file
Line Input #FileHandle, TextLine$ ' Read line into variable
If InStr(TextLine$, "=") > 0 Then
strUserReg = Mid(TextLine$, InStr(TextLine$, "=") + 2)
End If
Loop
End If
Loop
Close #FileHandle
Me.Label1.Caption = strUserName
Me.Label2.Caption = strUserReg
End Sub
And it works perfectly.. Thanks a lot!
You're going in the credits section of the program :)
Reagrds
Rhys
|
|
 |