Results 1 to 21 of 21

Thread: Reading form text file (RESOLVED)

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Re: Reading form text file

    Are you guys talking about the split function by manavo11? I have that,
    but I still get the same error. Any ideas?

  2. #2
    Member seraphicmortal's Avatar
    Join Date
    May 2005
    Posts
    56

    Smile Re: Reading form text file

    Hi RD Cody! I had that kind of program as well.. why don't you give this a try.. These codes saves the text being inputted by the user in the textbox in a file (which the file extension can btw be specified by you) and the user can retrieve the file and show the same text and order back to the form where your textboxes are... Quite a long explanation eyh.. You can tweek it to suit your needs if you want.

    I made a class, which I named namelist.
    This class has properties namely:
    name
    symbol
    quantity

    My example.plp output would look like this when opened in a text editor(like notepad):
    [Name] x32
    [Quantity] x75
    [Symbol] q21


    (globalvar.bas)
    VB Code:
    1. Option Explicit
    2.  
    3. Public pl1 As New namelist

    (frm_field) 'This is the form where the textboxes are
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub fieldupload()
    4.     With pl1
    5.         .symbol = txt_sym.Text
    6.         .name = txt_name.Text
    7.         .quantity = txt_qty.Text
    8.     End With
    9. End Sub
    10.  
    11. Private Sub cmd_apply_Click()
    12.     If Len(txt_sym.Text) = 0 Or _
    13.        Len(txt_name.Text) = 0 Or _
    14.        Len(txt_qty.Text) = 0 Or _
    15.         MsgBox "please enter the necessary values."
    16.         txt_sym.SetFocus
    17.     Else
    18.         fieldupload
    19.         MsgBox ("Creating profile...  Please choose what directory to save your profile.")
    20.         frm_dir.Caption = "Save Profile"
    21.         frm_dir.Show vbModal
    22.     End If
    23. End Sub
    24.  
    25. Function GetString(tmpStr As String, tmpDiv As String, Mode As String) As String
    26. Dim tmpRetStr As String
    27. Dim tmpLen As Integer
    28. Dim tmpErr As Integer
    29.   ' *****************************************************
    30.   ' tmpString = The whole string
    31.   ' tmpDiv = The Divider chr
    32.   ' if mode = "F" then get the string in front of the div
    33.   '
    34.   ' Return values:
    35.   '  Errors
    36.   '    Err1 = wrong mode
    37.   '    Err2 =  No Div, did not found a divider
    38.   '    Err5 = No String
    39.   '    Err6 = No C, did not find any string in the middle
    40.   '  Normal
    41.   '    String
    42.   ' ******************************************************
    43.   ' Example: GetString("Red=Green","=","F") will retrun "Red"
    44.   ' *** for string
    45.   tmpErr = 0
    46.   If Len(tmpStr) = 0 Then
    47.     tmpErr = 5
    48.     GoTo GetStringErr
    49.   End If
    50.  
    51.   ' *** test for chr in tmpDiv
    52.   If Len(tmpDiv) = 0 Then
    53.     tmpErr = 2
    54.     GoTo GetStringErr
    55.   End If
    56.  
    57.   ' *** test for div in string
    58.   If InStr(1, tmpStr, tmpDiv) = 0 Then
    59.     tmpErr = 2
    60.     GoTo GetStringErr
    61.   End If
    62.  
    63.   If Mode = "C" Then
    64.      tmpRetStr = Mid(tmpStr, 9)
    65.      If Len(tmpRetStr) > 0 Then
    66.          GetString = tmpRetStr
    67.          Exit Function
    68.      Else
    69.          tmpErr = 6
    70.          'GoTo GetStringErr
    71.          MsgBox ("Error: Non-standard format in saving profile name.  No hyphen '-' found.")
    72.          frm_dir.txt_plprofile.Text = "*.plp"
    73.      End If
    74.          MsgBox ("Error: Non-standard format in saving profile name.  No hyphen '-' found.")
    75.          frm_dir.txt_plprofile.Text = "*.plp"
    76.   End If
    77.  
    78.     tmpErr = 1
    79.     frm_dir.txt_plprofile.Text = "*.plp"
    80.     'GoTo GetStringErr
    81.   Exit Function
    82. GetStringErr:
    83. GetString = "Err" & tmpErr
    84. End Function
    85.  
    86.  
    87. Private Sub loadfield()
    88.     txt_sym.Text = pl1.symbol
    89.     txt_name.Text = pl1.name
    90.     txt_qty.Text = pl1.quantity
    91. End Sub
    92.    
    93. Private Sub cmd_back_Click()
    94.     Form_Load
    95.     Me.Hide
    96.     frm_loadfile.Show vbModal
    97. End Sub
    98.  
    99. Private Sub cmd_next_Click()
    100.     Me.Hide
    101.     frm_summary.Show vbModal ' another form which basically are just labels that shows a summmay of the information
    102. End Sub
    103.  
    104. Private Sub cmd_retrieve_Click()
    105.     frm_dir.file1.Enabled = True
    106.     loadfield
    107.    
    108.     frm_dir.Caption = "Load Profile"
    109.     frm_dir.Show vbModal
    110. End Sub
    111.  
    112. Private Sub Form_Load()
    113. On Error GoTo Form_Load_Error
    114. 'This code sets up the Text Boxes
    115.     cmd_next.Enabled = False
    116.     Me.txt_name.Text = ""
    117.     Me.txt_qty.Text = ""
    118.     Me.txt_sym.Text = ""
    119. On Error GoTo 0
    120. Exit Sub
    121.  
    122. Form_Load_Error:
    123. MsgBox "Error " & Err.Number & " (" & Err.description & ") in procedure Form_Load of Form " & Me.Name
    124. End Sub
    125.  
    126. Private Sub Validate_Entry(ByRef txtBox As TextBox)
    127.  
    128.     If Left$(txtBox.Text, 1) <> vbNullString Then
    129.         If UCase(txtBox.Text) Like "[A-Z]" & String(Len(txtBox.Text) - 1, "#") And Len(txtBox.Text) < 6 Then
    130.             txtBox.Text = UCase(txtBox.Text)
    131.             txtBox.SelStart = Len(txtBox.Text)
    132.         Else
    133.             MsgBox "Please try again.", vbOKOnly + vbInformation, "Invalid Entry"
    134.             txtBox.Text = ""
    135.         End If
    136.     End If
    137. End Sub
    138.  
    139. Private Sub txt_qty_Change()
    140.     Call Validate_Entry(txt_qty)
    141. End Sub
    142.  
    143. Private Sub txt_sym_Change()
    144.     Call Validate_Entry(txt_sym)
    145. End Sub
    146.  
    147. Private Sub txt_name_Change()
    148.     Call Validate_Entry(txt_name)
    149. End Sub

    now to save the text inputted in the textbox in a file....use this
    (frm_dir)

    VB Code:
    1. Option Explicit
    2. Public profilepath As String
    3. Public finalprofile As String
    4.  
    5. Private Sub Form_Load()
    6. Dim prof As String
    7.     txt_profilepath.Enabled = False
    8.     file1.Enabled = False
    9.     txt_plprofile.Text = frm_loadfile.var1
    10.     prof = frm_field.GetString(txt_plprofile.Text, "-", "C")
    11.     txt_plprofile.Text = prof + ".plp"
    12.      'txt_plprofile.Text = prof + ".plp"
    13. End Sub
    14.  
    15. Private Sub cmd_cancel_Click()
    16.     MsgBox "Cancelled"
    17.     Unload Me
    18. End Sub
    19.  
    20. Private Sub cmd_ok_Click()
    21. Dim prof As String
    22.  
    23. Select Case Me.Caption
    24.     Case "Save Profile"
    25.     txt_profilepath.Text = file1.Path & "\" & file1.FileName & txt_plprofile.Text
    26.         If isFile(txt_profilepath.Text) Then
    27.             txt_plprofile.Text = frm_loadfile.var1
    28.             prof = frm_field.GetString(txt_plprofile.Text, "-", "C")
    29.                 If MsgBox("File " & txt_profilepath & " exists.  Do you want to overwrite the current profile?{Y/N]", vbQuestion + vbYesNo, App.ProductName + " Prompt") = vbYes Then
    30.                     SaveProfile txt_profilepath.Text, pl1
    31.                     finalprofile = txt_plprofile.Text
    32.                 Else
    33.                     txt_profilepath.Text = file1.Path & file1.FileName & "\" & prof & Format(Now, "mmddyyyyhhmmss") & ".plp"
    34.                     SaveProfile txt_profilepath.Text, pl1
    35.                     finalprofile = txt_plprofile.Text
    36.                 End If
    37.         Else
    38.             MsgBox "Saving " & txt_profilepath & " as new profile."
    39.             SaveProfile txt_profilepath.Text, pl1
    40.             finalprofile = txt_plprofile.Text
    41.         End If
    42.  
    43.         profilepath = txt_profilepath.Text
    44.         frm_field.cmd_next.Enabled = True
    45.  
    46.     Case "Load Profile"
    47.         txt_profilepath.Text = file1.Path & "\" & file1.FileName
    48.         MsgBox "Loading " & txt_profilepath & " profile."
    49.             LoadProfile txt_profilepath.Text, pl1
    50.         profilepath = txt_profilepath.Text
    51.         frm_field.cmd_next.Enabled = True
    52. End Select
    53.     Me.Hide
    54. End Sub
    55.  
    56. Private Sub LoadProfile(WhatFile As String, TheProfile As partslist)
    57. Dim FreeFileHandle  As Integer, a$, B() As String
    58.  
    59.     FreeFileHandle = FreeFile
    60.     Open WhatFile For Input As FreeFileHandle
    61.     With TheProfile
    62.         Line Input #FreeFileHandle, a ' "[Symbol] " & .symbol
    63.         B = Split(a, " ")
    64.         .symbol = B(1)
    65.         frm_field.txt_sym.Text = .symbolic
    66.        
    67.         Line Input #FreeFileHandle, a ' "[Name] " & .Name
    68.         B = Split(a, " ")
    69.         .name = B(1)
    70.         frm_field.txt_name.Text = .name
    71.        
    72.         Line Input #FreeFileHandle, a '  "[Quantity] " & .quantity
    73.         B = Split(a, " ")
    74.         .quantity = B(1)
    75.         frm_field.txt_qty.Text = .quantity
    76.  
    77.     End With
    78.     Close FreeFileHandle
    79. End Sub
    80.  
    81.  
    82. Private Sub SaveProfile(WhatFile As String, TheProfile As namelist)
    83. Dim FreeFileHandle  As Integer
    84.  
    85.     FreeFileHandle = FreeFile
    86.     Open WhatFile For Output As FreeFileHandle
    87.     With TheProfile
    88.         Print #FreeFileHandle, "[Name] " & .name
    89.         Print #FreeFileHandle, "[Quantity] " & .quantity
    90.         Print #FreeFileHandle, "[Symbolic] " & .symbol
    91.     End With
    92.     Close FreeFileHandle
    93. End Sub
    94.  
    95. Private Sub dir1_Change()
    96.     file1.Path = dir1.Path
    97. End Sub
    98.  
    99. Private Sub drive1_Change()
    100.     dir1.Path = drive1.Drive
    101. End Sub
    102.  
    103. Private Sub file1_PathChange()
    104.     Me.Caption = "Dialog Box - [" & file1.Path & "]"
    105. End Sub
    106.  
    107. Private Sub file1_Click()
    108.     txt_plprofile.Text = file1.FileName 'file1 is a file list box
    109. End Sub
    110.  
    111. Function isFile(ByVal sFileName As String) As Integer
    112.   On Error Resume Next
    113.   Dim lFileLength As Long
    114.   Const ATTR_NORMAL = 0
    115.  
    116.   isFile = False
    117.   If Dir$(sFileName, ATTR_NORMAL) <> "" Then
    118.   lFileLength = FileLen(sFileName)
    119.   If (lFileLength > 0) Then isFile = True
    120.   End If
    121. End Function


    I hope this helps..
    Yoroshiku,
    seraphicmortal


    ______________________________________________________
    Thirst for knowledge can never be quenched...Ask for more!!.


    Oh! Please..Show your appreciation by clicking if you deem this post helpful.
    Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width