Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

VB File Handling....

Options
  • 09-04-2002 4:16pm
    #1
    Closed Accounts Posts: 7,230 ✭✭✭


    Ok lets say i have five variables of type String. I need to be able to put the values that these variables hold into a file and was wondering how do i do this? What i do is i ask the user 5 questions, each answer is assigned to a variable, and then printed to a file. please help!!! Thanks in advance!@#


Comments

  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Here's a quick sub with sVar1 to sVar5 as your variables and sLogFileName as the text file you want to save them to (in a CSV format).
    Public Sub logVars(sVar1 As String, sVar2 As String, sVar3 As String, _
        sVar4 As String, sVar5 As String, sLogFileName As String)
        
        Dim Temp As String
        Dim i As Integer
        
        i = FreeFile
        Temp = """" & sVar1 & """,""" & sVar2 & """,""" & sVar3 & ""","""
        Temp = Temp & sVar4 & """,""" & sVar5 & """"
            
        Open sLogFileName For Append As #i
            Print #i, Temp
        Close #i
    End Sub
    


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    thanks!


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Looks a bit messy. Wouldn't this be better?
    
    Public Sub logVars(sVar() As String, sLogFileName As String)
    
        total = ubound(sVar) 
    
        QUOTE = chr$(34)
    
        ff = freefile
        open sLogFileName for append as #ff
    
          for n = 1 to total
               print #ff, QUOTE;sVar(n); QUOTE;
               if n < total then 
                     print #ff, ","
               else
                     print #ff, ""
               endif
          next n
    
       close #ff
    
    end sub
    

    Although I'd do

    Public Sub logVars(sVar() As String, fileHandle As Integer)

    and open up the file and call the sub to write the record.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Hmmpfh... Show off... At least I declared my variables ;)

    (btw, just noticed the else in your code is superfluous)


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Originally posted by The Corinthian
    Hmmpfh... Show off... At least I declared my variables ;)

    (btw, just noticed the else in your code is superfluous)

    Yes Bold Hobbes should declare variables! I even put an "option explicit" normally to force me to.

    As for the ELSE, it's needed but there is a bug there. It should be...
               if n < total then 
                     print #ff, ","[b];[/b]
               else
                     print #ff, ""
               endif
    

    This tells it to add a comma to the line except at the last variable where it will print a 0x0d0a (CRLF)


  • Advertisement
  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Yes, I see. Still I'd probably not bother with the ELSE and just put the print #ff, "" just after closing the FOR/NEXT loop:
    for n = 1 to total
        print #ff, QUOTE;sVar(n); QUOTE;
        if n < total then print #ff, ","
    next n
    print #ff, "" 
    
    Better still, wouldn't a FOR/EACH be better than using FOR/NEXT?

    101 Ways to skin a cat, I expect...


  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    Geez lads, get a room ffs :)

    Al.


Advertisement