write to file

Discussion in 'AutoCAD' started by robert vetrano, Jul 30, 2004.

  1. Hi
    Writing to a file using write statment. Using an * as a flag for the name.
    When I try to read the file and display in list box I get nothing. If I
    manually remove the " from around the string in the file it works fine. I've
    tried different variation left$ and right$ with no success. Without going
    through a long code of removing this and that character is there a simple
    way to remove the " and read the * as the1st character.
    Thanks
    bob Vetrano

    FILE READS

    "*sas"
    3,3,4
    "*yuyu"
    3,21,4
    "*sdsd"
    3,21,34

    CODE TO WRITE IS:
    Private Sub cmdSave_Click()
    Dim File1 As Integer
    Dim Name As String, A As Double, B As Double, C As Double

    On Error GoTo err_handler

    File1 = FreeFile

    'get data
    Name = "*" & txtname.Text
    A = Val(txtA.Text)
    B = Val(txtB.Text)
    C = Val(txtC.Text)

    'open file for append
    Open "c:\vba\Data1.mem" For Append As #File1

    'write to file
    Write #File1, Name
    Write #File1, A, B, C
    Close #File1
    Exit Sub

    err_handler:
    MsgBox Err.Number & " " & Err.Description
    Err.Clear

    Exit Sub
    End Sub
    CODE TO READ IS:

    Public Sub cmdSelect_Click()

    lbxNameSelect.Visible = True
    Dim sTempa As String
    Dim nFilea As Integer

    On Error GoTo err_handler

    nFilea = FreeFile

    lbxNameSelect.Clear

    Open "c:\VBA\data1.mem" For Input As #nFilea

    While Not EOF(nFilea)
    Line Input #nFilea, sTempa

    If Left(sTempa, 1) = "*" Then
    sTempa = Right$(sTempa, (Len(sTempa) - 1))

    lbxNameSelect.AddItem sTempa

    End If
    Wend

    Close #nFilea

    Exit Sub

    err_handler:
    MsgBox "error No" & Err.Number & "-" & Err.Description
    Err.Clear
    Exit Sub

    End Sub
     
    robert vetrano, Jul 30, 2004
    #1
  2. If Left(String, 1) = "," Then
    String = Right$(String, (Len(String) - 1))
     
    Paul Richardson, Jul 30, 2004
    #2
  3. Sorry thought I saw ","

    try this...It looks for a space in a line of text and checks the first two
    letters in the word
    before the space. If they = myQuote remove a character..ie the "

    Const myQuote = """" + "*"
    string = Left(textLine, InStr(1, textLine, " "))
    firstChar = Left$(firstWord, 2)
    Select Case firstChar
    Case Is = myQuote
    String = Right$(String, (Len(String) - 1))
    End Select
     
    Paul Richardson, Jul 30, 2004
    #3
  4. If you use Print# instead of Write#, it will not add the quotes. Keep the
    Write for the A,B,C part or else you'd have to add the commas yourself....

    James
     
    James Belshan, Jul 30, 2004
    #4
  5. sorry it's Fryday. Long week of parcing text files

    Should of tested, I used "firstWord" instead of "string" in this line
    Should be.... firstChar = Left$(string, 2)

    {|:)
     
    Paul Richardson, Jul 30, 2004
    #5
  6. James
    Once again didn't look to the simpliest method. someone should write a VBA
    for Dummies.

    Thanks
    bob v
     
    robert vetrano, Jul 31, 2004
    #6
  7. robert vetrano

    Jürg Menzi Guest

    Jürg Menzi, Jul 31, 2004
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.