Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Opening Files in VB

How do I open a file up in VB like a .TXT file by clicking a command button?


I've already tried using the shell command but no luck =( thanx,


RemiX


Comments

  • Ooops I'm also using VB4.0 Standard Editon.


    Thanks,


    RemiX


  • : Ooops I'm also using VB4.0 Standard Editon.

    : Thanks,

    : RemiX


    --------------------------------------------------------------




    Well, here are two functions, Saving text and reading text from a file.

    This is just an example i took from one of my older applications,

    normally you will scan the file using Line Input with a "while"-struct,

    to get all lines. Look up the functions in your help. EOF() might be

    useful, too.


    '-------------------------------------------------------------

    Public Function SaveFile(FileName As String, FileContents As String) As Boolean

    '-------------------------------------------------------------

    Dim FreeFileNumber As Long


    On Error GoTo FileError

    'Get FileHandle

    FreeFileNumber = FreeFile

    'Open File

    Open FileName For Output As FreeFileNumber

    'Write Text to File

    Print #FreeFileNumber, FileContents

    'Close File

    Close FreeFileNumber

    'File saved

    SaveFile = True

    Exit Function


    FileError:

    'File NOT Saved

    SaveFile = False

    End Function


    '-------------------------------------------------------------

    Public Sub nvLoadDefaultPathNamesFromWorkDir()

    '-------------------------------------------------------------

    Dim FreeFileNumber As Long


    On Error GoTo nvLoadAlternative


    FreeFileNumber = FreeFile


    'Open for Reading

    Open App.Path & "DefaultPath.DAT" For Input As FreeFileNumber

    'Read Lines into Strings (globals here, sorry)

    Line Input #FreeFileNumber, DefaultParameterFilePath

    Line Input #FreeFileNumber, DefaultFunctionFilePath

    Line Input #FreeFileNumber, DefaultErrorMSGFilePath

    Line Input #FreeFileNumber, DefaultRefTablePath

    Line Input #FreeFileNumber, DefaultJobPath

    'Close File

    Close FreeFileNumber

    On Error GoTo 0

    'Normal Function end

    Exit Sub


    nvLoadAlternative:

    'Use default directories

    DefaultParameterFilePath = App.Path

    DefaultFunctionFilePath = App.Path

    DefaultErrorMSGFilePath = App.Path

    DefaultRefTablePath = App.Path & "" 'Important for C/C++ DLL

    DefaultJobPath = App.Path

    End Sub




    Hope this helps,

    -nv


  • To open a file and retrieve the contents, you can use:


    Dim FileContents As String

    Open MyFile For Input As 1

    'Won't work if EOF char exists in middle of file.

    FileContents = Input(LOF(1), 1)

    Close


    -or-


    'Following code is actually quite fast.

    'For files longer than about 100,000 bytes,

    'I would suggest adding another set for a string

    'with a length of 10,000 to speed things up.

    Dim FileContents As String

    Dim Temp As String

    Dim LargeChunk As String * 1000

    Dim SmallChunk As String * 100

    Dim SingleChar As String * 1

    Dim NumLarge As Integer

    Dim NumSmall As Integer

    Dim NumSingle As Integer

    Open MyFile For Binary As 1

    TotalFile = LOF(1)

    NumLarge = TotalFile 1000

    TotalFile = TotalFile - NumLarge * 1000

    NumSmall = TotalFile 100

    TotalFile = TotalFile - NumSmall * 100

    NumSingle = TotalFile

    If NumLarge > 0 Then

    For I = 1 To NumLarge

    Get #1, , Temp

    FileContents = FileContents & Temp

    Next

    End If

    If NumSmall > 0 Then

    For I = 1 To NumSmall

    Get #1, , Temp

    FileContents = FileContents & Temp

    Next

    End If

    If NumSingle > 0 Then

    For I = 1 To NumSingle

    Get #1, , Temp

    FileContents = FileContents & Temp

    Next

    End If

    Close




    Hope this helps!


  • I'm not quite sure if in VB4 you have the Rich Text box, if you have it... use it... One of it's special features is that it's capable of opening a file...


  • Justin BibJustin Bib USAMember Posts: 0

    _ [ http://forcoder.org ] free ebooks and video tutorials about \ R, Scratch, C#, PHP, Ruby, Java, Delphi, PL/SQL, Visual Basic .NET, Objective-C, Perl, Assembly, MATLAB, C, Swift, Python, Go, Visual Basic, JavaScript, C++ Awk, LabVIEW, Scheme, F#, Erlang, Fortran, Ada, Lua, Alice, Crystal, Clojure, Julia, FoxPro, VBScript, COBOL, Transact-SQL, Logo, Rust, Bash, Lisp, Scala, Dart, Kotlin, D, Prolog, SAS, ML, Apex, Hack, ABAP \ ________

Sign In or Register to comment.