Retrieve data from a text file and display in a textbox.
Hi! :wave: I hope someone could help me with this.
I want to retrieve data saved in .pnp file and display it in the same txtboxes and same order as it was saved.
My current code is:
VB Code:
(globalvar.bas)
Option Explicit
Public pn1 As New partnamelist
(frm_field)
Private Sub fieldupload()
With pn1 'I made a class named partnamelist having these 4 properties: part, description, quantity, and version
.part = txt_part.text
.description = txt_desc.Text
.quantity = txt_qty.Text
.version = txt_version.Text
End With
End Sub
Private Sub cmd_apply_Click()
dim filename as string
If Len(txt_part.text) = 0 And _
Len(txt_desc.Text) = 0 And _
Len(txt_qty.Text) = 0 And _
Len(txt_version.Text) = 0 Then
MsgBox "please enter the necessary values."
txt_part.setfocus
Else
fieldupload
filename = txt_filename.text 'Specified filename of the user
MsgBox ("Creating profile... Please choose what directory to save your profile.")
frm_dir.Show vbModal
End If
End Sub
'(frm_dir)
Private Sub Form_Load()
txt_pnprofile.Text = frm_field.filename + ".pnp" '--> "pnp" here in my example is my chosen extension.
End Sub
Private Sub cmd_ok_Click()
txt_profilepath.Text = file1.Path & file1.FileName & "\" & txt_plprofile.Text 'file1 is a file listbox btw
MsgBox "Saving " & txt_profilepath & " as new profile."
SaveProfile txt_profilepath.Text, pn1
profilepath = txt_profilepath.Text
End Sub
Private Sub SaveProfile(WhatFile As String, TheProfile As namelist)
Dim FreeFileHandle As Integer
FreeFileHandle = FreeFile
Open WhatFile For Output As FreeFileHandle
With TheProfile
Print #FreeFileHandle, "[Part] " & .part
Print #FreeFileHandle, "[Description] " & .description
Print #FreeFileHandle, "[Quantity] " & .quantity
Print #FreeFileHandle, "[Version] " & .version
End With
Close FreeFileHandle
End Sub
Output of egfile.pnp:
[Description] b42
[MakerCode] c65
[PartnameCode] x32
[Quantity] x75
[SymbolicCode] q21
After clicking RETRIEVE button, I want that b42 will be displayed in txt_description.text and so on and so forth.
How do I do this? I don't know how to use Line Input # Statement as well.
Anybody, pls. help. :confused:
Re: Retrieve data from a text file and display in a textbox.
Have you tried soemthing like this:
VB Code:
(globalvar.bas)
Option Explicit
Public pn1 As New partnamelist
(frm_field)
Private Sub fieldupload()
With pn1 'I made a class named partnamelist having these 4 properties: part, description, quantity, and version
.part = txt_part.text
.description = txt_desc.Text
.quantity = txt_qty.Text
.version = txt_version.Text
End With
End Sub
Private Sub cmd_apply_Click()
dim filename as string
If Len(txt_part.text) = 0 And _
Len(txt_desc.Text) = 0 And _
Len(txt_qty.Text) = 0 And _
Len(txt_version.Text) = 0 Then
MsgBox "please enter the necessary values."
txt_part.setfocus
Else
fieldupload
filename = txt_filename.text 'Specified filename of the user
MsgBox ("Creating profile... Please choose what directory to save your profile.")
frm_dir.Show vbModal
End If
End Sub
'(frm_dir)
Private Sub Form_Load()
txt_pnprofile.Text = frm_field.filename + ".pnp" '--> "pnp" here in my example is my chosen extension.
End Sub
Private Sub cmd_ok_Click()
txt_profilepath.Text = file1.Path & file1.FileName & "\" & txt_plprofile.Text 'file1 is a file listbox btw
MsgBox "Saving " & txt_profilepath & " as new profile."
SaveProfile txt_profilepath.Text, pn1
profilepath = txt_profilepath.Text
End Sub
Private Sub SaveProfile(WhatFile As String, TheProfile As namelist)
Dim FreeFileHandle As Integer
FreeFileHandle = FreeFile
Open WhatFile For Output As FreeFileHandle
With TheProfile
Print #FreeFileHandle, "[Part] " & .part
txt_part.Text = .part
Print #FreeFileHandle, "[Description] " & .description
txt_description.Text = .description
Print #FreeFileHandle, "[Quantity] " & .quantity
txt_quantity.Text = .quantity
Print #FreeFileHandle, "[Version] " & .version
txt_version.Text = .version
End With
Close FreeFileHandle
End Sub
Not shure but I think that should work :)
Cheers,
RyanJ
Re: Function to retrieve data from a text file and display in a textbox.
I wana know what function to use that would retrieve the data being inputted in the textbox (which is saved in a .pnp file)....
Can anyone help?
Re: Retrieve data from a text file and display in a textbox.
Here is one way to do it:
EDIT: It changed!
VB Code:
Private Sub LoadProfile(WhatFile As String, TheProfile As namelist)
Dim FreeFileHandle As Integer, A$,B() as String
FreeFileHandle = FreeFile
Open WhatFile For Input As FreeFileHandle
With TheProfile
LineInput #FreeFileHandle, a ' "[Part] " & .part
b=split(a," ")
.part = b(1)
txt_part.Text = .part
LineInput #FreeFileHandle, a ' "[Description] " & .description
b=split(a," ")
.description = b(1)
txt_description.Text = .description
LineInput #FreeFileHandle, a ' "[Quantity] " & .quantity
b=split(a," ")
.quantity = b(1)
txt_quantity.Text = .quantity
LineInput #FreeFileHandle, a ' "[Version] " & .version
b=split(a," ")
.version = b(1)
txt_version.Text = .version
End With
Close FreeFileHandle
End Sub
Re: Retrieve data from a text file and display in a textbox.
Hi! Dglienna, I tried your suggestion but even if I haven't compiled it yet, the lines below turns red.(what's the error in this?)
VB Code:
Private Sub LoadProfile(WhatFile As String, TheProfile As namelist)
Dim FreeFileHandle As Integer, A$,B() as String
FreeFileHandle = FreeFile
Open WhatFile For Input As FreeFileHandle
With TheProfile
[COLOR=Red]LineInput #FreeFileHandle, a ' "[Part] " & .part[/COLOR]
b=split(a," ")
.part = b(1)
txt_part.Text = .part
[COLOR=Red]LineInput #FreeFileHandle, a ' "[Description] " & .description[/COLOR]
b=split(a," ")
.description = b(1)
txt_description.Text = .description
[COLOR=Red]LineInput #FreeFileHandle, a ' "[Quantity] " & .quantity[/COLOR]
b=split(a," ")
.quantity = b(1)
txt_quantity.Text = .quantity
[COLOR=Red]LineInput #FreeFileHandle, a ' "[Version] " & .version[/COLOR]
b=split(a," ")
.version = b(1)
txt_version.Text = .version
End With
Close FreeFileHandle
End Sub
Re: Retrieve data from a text file and display in a textbox.
Sorry about that. I didn't test it first.
Just add a space to the lines in RED! :)
Re: Retrieve data from a text file and display in a textbox.
Oooh! My bad....didn't see that as well...hehehe =)
I tried compiling it though after putting the space and it says
Run Time Error '76':
Path Not Found.
Re: Retrieve data from a text file and display in a textbox.
You have to specifiy the path to the file you want to load when you call the procedure.
Re: Retrieve data from a text file and display in a textbox.
I did do that Baja.. :( Yet I don't understand why it says Path not Found.
Let me walk you through.
If a user clicks RETRIEVE button, it will go to a frm_dir (directory form), if the profile he wants exists....all he has to do is click the file and and press OK (by doing this, the user is specifying what file (profile) to be loaded, i think...). Then it will display
txt_description.text = b42
txt_MakerCode.text = c65
txt_PartnameCode.text = x32
txt_Quantity.text = x75
txt_SymbolicCode.text = q21
anyone, help me. :confused:
Re: Retrieve data from a text file and display in a textbox.
What are you using to allow user to select the file, and how?
Re: Retrieve data from a text file and display in a textbox.
The best solution to debug this would be to set a break point somewhere before the call of the procedure, and then use F8 key to step through the code line by line and see exactly what data get passed as the Path (WhatFile) parameter.
Re: Retrieve data from a text file and display in a textbox.
OK button from the form directory (frm_dir).
VB Code:
Private Sub cmd_ok_Click()
txt_profilepath.Text = file1.Path & file1.FileName & "\" & txt_plprofile.Text
Select Case Me.Caption
Case "Save Profile"
If isFile(txt_profilepath.Text) Then
If MsgBox("File " & txt_profilepath & " exists. Do you want to overwrite the current profile?{Y/N]", vbQuestion + vbYesNo, App.ProductName + " Prompt") = vbYes Then
SaveProfile txt_profilepath.Text, pl1
Else
txt_profilepath.Text = file1.Path & file1.FileName & "\" & txt_plprofile.Text & "-02"
SaveProfile txt_profilepath.Text, pl1
End If
Else
MsgBox "Saving " & txt_profilepath & " as new profile."
SaveProfile txt_profilepath.Text, pl1
End If
profilepath = txt_profilepath.Text
frm_field.cmd_next.Enabled = True
Case "Load Profile"
MsgBox "Loading " & txt_profilepath & " profile."
LoadProfile txt_profilepath.Text, pl1
End Select
Me.Hide
End Sub
I think it's in this code where the user specifies what file (or profile) to load.
Re: Retrieve data from a text file and display in a textbox.
I am not sure about this, but I think you need to put a slash between these Path and FileName:
txt_profilepath.Text = file1.Path & "\" & file1.FileName
Re: Retrieve data from a text file and display in a textbox.
WhatFile points to the correct file. I don't why it says 'Path not Found'... =(
Re: Retrieve data from a text file and display in a textbox.
Also, you can put this line:
MsgBox txt_profilepath.Text
after this line:
txt_profilepath.Text = file1.Path & file1.FileName & "\" & txt_plprofile.Text
That way you will see if the path looks OK.
Re: Retrieve data from a text file and display in a textbox.
If the path is ok, then the problem is in your loading procedure (LoadProfile). If it is then post it's code here.
Re: Retrieve data from a text file and display in a textbox.
Still Path Not Found Baja...but the slash was something to be added as well. Thanx!
Re: Retrieve data from a text file and display in a textbox.
The file gets loaded here;
What is this: txt_plprofile.Text
that should be the file name to load.
Re: Retrieve data from a text file and display in a textbox.
Quote:
Originally Posted by baja_yu
Also, you can put this line:
MsgBox txt_profilepath.Text
after this line:
txt_profilepath.Text = file1.Path & file1.FileName & "\" & txt_plprofile.Text
That way you will see if the path looks OK.
Use that, it will show you what you are trying to load. If that is ok then the error is elswhere.
Re: Retrieve data from a text file and display in a textbox.
Ahhhhhh!!! Silly me!! Path not found since I placed file1.FileName & "\" & txt_plprofile.text when in fact file1.fileName and txt_plprofile.text means the same!!!! :blush:
Thanks a lot!!!!! :D