Page 1 of 2 12 LastLast
Results 1 to 40 of 46

Thread: how to open a file in VB?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40

    how to open a file in VB?

    thank u

  2. #2
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345
    what type of file?? if its a text file to a text box then use:
    VB Code:
    1. Open "C:\File.txt" For Input as #1
    2.       Text1.Text = Input(LOF(1),1)
    3.     Close #1
    4.   End With

  3. #3
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    VB Code:
    1. Open "C:\Path\Filename.ext" For Input As #1

    VB Code:
    1. Open "C:\Path\Filename.ext" For Output As #1


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40
    Private Sub help_Click()
    Open "d:\223\234.chm" For Output As #1

    End Sub

    but no any program was opened?

  5. #5
    Member BinaryAnge's Avatar
    Join Date
    Jun 2002
    Location
    Columbus,Ohio
    Posts
    51
    use file system objects

    dim FSO as new filesystemobject
    dim TS as textstream

    Set TS = FSO.OpenTextFile(MyFile, ForReading) or forwriting or forappending
    The Programmers Credo -
    Protect dumb-ass from himself.

  6. #6
    Member BinaryAnge's Avatar
    Join Date
    Jun 2002
    Location
    Columbus,Ohio
    Posts
    51

    then

    ts.write (whatever)

    ts.read(length as long)
    The Programmers Credo -
    Protect dumb-ass from himself.

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Use "F1":
    Open Statement


    Enables input/output (I/O) to a file.

    Syntax

    Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]

    The Open statement syntax has these parts:

    Part Description
    pathname Required.String expression that specifies a file name — may include directory or folder, and drive.
    mode Required.Keyword specifying the file mode: Append, Binary, Input, Output, or Random. If unspecified, the file is opened for Random access.
    access Optional. Keyword specifying the operations permitted on the open file: Read, Write, or Read Write.
    lock Optional. Keyword specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.
    filenumber Required. A validfile number in the range 1 to 511, inclusive. Use the FreeFile function to obtain the next available file number.
    reclength Optional. Number less than or equal to 32,767 (bytes). For files opened for random access, this value is the record length. For sequential files, this value is the number of characters buffered.


    Remarks

    You must open a file before any I/O operation can be performed on it. Open allocates a buffer for I/O to the file and determines the mode of access to use with the buffer.

    If the file specified by pathname doesn't exist, it is created when a file is opened for Append, Binary, Output, or Random modes.

    If the file is already opened by another process and the specified type of access is not allowed, the Open operation fails and an error occurs.

    The Len clause is ignored if mode is Binary.

    Important In Binary, Input, and Random modes, you can open a file using a different file number without first closing the file. In Append and Output modes, you must close a file before opening it with a different file number.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  8. #8
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Originally posted by flyflydream2002
    Private Sub help_Click()
    Open "d:\223\234.chm" For Output As #1

    End Sub

    but no any program was opened?
    Ah, you mean you want to Execute a file. use the Shell function:

    VB Code:
    1. ProcessID = Shell("d:\223\234.chm", vbNormalFocus)


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  9. #9
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Originally posted by BinaryAnge
    use file system objects

    dim FSO as new filesystemobject
    dim TS as textstream

    Set TS = FSO.OpenTextFile(MyFile, ForReading) or forwriting or forappending
    It's not worth it. If you do do this, then you would have to make sure the end user has VBScript installed (OK...few system doesn't, but just in case...)


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  10. #10
    Member BinaryAnge's Avatar
    Join Date
    Jun 2002
    Location
    Columbus,Ohio
    Posts
    51
    true, who ever is writing the program would need to add a reference to "microsoft scripting runtime". and if they dont have that, they should just use a pencil and paper.
    The Programmers Credo -
    Protect dumb-ass from himself.

  11. #11
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Scripting Runtime is included in VBScript.

    Anyways, I (and many other people on the forums) usually prefer the Open statement for its ability to write binary/random files with ease (and give you control, too). It's much better than writing on pencil and paper, and certainly better than messing with the file APIs in VB (I've only used them in C++ programming, never VB).


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  12. #12
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Try to avoid FSO whenever you can!!
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  13. #13
    Member BinaryAnge's Avatar
    Join Date
    Jun 2002
    Location
    Columbus,Ohio
    Posts
    51
    the pencil and paper part was a joke because vbscripting in so common.
    The Programmers Credo -
    Protect dumb-ass from himself.

  14. #14
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    Originally posted by Mc Brain
    Try to avoid FSO whenever you can!!
    Sage advice.


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  15. #15
    Member BinaryAnge's Avatar
    Join Date
    Jun 2002
    Location
    Columbus,Ohio
    Posts
    51
    why do you want to avoid fso?
    The Programmers Credo -
    Protect dumb-ass from himself.

  16. #16
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Let's quote TurtleTips (I'm too lazy right now to write my own words):
    I am famed on VBF for constantly trashing the FileSystemObject, or FSO. The FSO facilitates many file I/O functions. Only problem is most of the time, these functions are overly complicated by using the FSO. More importantly, the FSO adds a several hundred kilobyte dependency to your program, something that 56k users will not appreciate.

    Whenever possible, use the intrinsic file I/O functions, including Open, Close, Input, Write, Print, EOF, LOF, Kill, etc.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  17. #17
    Member BinaryAnge's Avatar
    Join Date
    Jun 2002
    Location
    Columbus,Ohio
    Posts
    51
    yea, i guess i see what youre saying. if you only need the basic functions, no need to over complicate.
    The Programmers Credo -
    Protect dumb-ass from himself.

  18. #18
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Not only that... Open, Close, Input, Write, Print, EOF, LOF, Kill, etc. are intrinsic I/O functions... you won't need any extra file to get your app working (besides VB runtimes, but you won't be able to get rid of them)
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  19. #19
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    And if there is something that the intruistic functions doesn't support (finding, for example), you can always use the Windows API. Sure, they require separate Windows DLLs, but it's highly unlikely that the end-user will be running your program without having user32, kernel32, shell32, gdi32, advapi, and those files.


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

  20. #20

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40
    visual basic code:-------------------------------------------------------------ProcessID = Shell("d:\223\234.chm", vbNormalFocus)
    -------------------------------------------------------------------------------
    But it's wrong!
    message:invalidable sub or parameter

  21. #21
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by flyflydream2002
    visual basic code:-------------------------------------------------------------ProcessID = Shell("d:\223\234.chm", vbNormalFocus)
    -------------------------------------------------------------------------------
    But it's wrong!
    message:invalidable sub or parameter
    Come again...

    what???
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  22. #22

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40
    I only want to open a help file when the people click help on menu.
    Can you understand me?

  23. #23
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Try:
    VB Code:
    1. Private Declare Function ShellExecute Lib "shell32.dll" Alias _
    2.   "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    3.   ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory _
    4.   As String, ByVal nShowCmd As Long) As Long
    5. Private Const SW_SHOWNORMAL = 1
    6.  
    7. Private Sub Command1_Click()
    8.   ShellExecute 0&, vbNullString, _
    9.     "d:\223\234.chm", _
    10.     vbNullString, "C:\", SW_SHOWNORMAL
    11. End Sub
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  24. #24

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40
    No.
    it's wrong.
    message:Don't open the file

  25. #25
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    It's not wrong... are you sure of the file's path!?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  26. #26

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40
    yes. I change that path to the correct path.
    the whole message:Don't open the file:mk:@MSITStore:d:\223\234.chm

  27. #27
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Stupid question.... can you even open the file by double-clicking over it?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  28. #28

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40
    No. How dare you say that!
    You can't answer my any question.but you can not say that! understand?

  29. #29
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    NO... I don't understand. What I also don't understand is why are you treating the way you're to someone who's trying to help you.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  30. #30
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    I have problems of my own... but I'm debugging them on my own. I'm not bothering people on this board.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  31. #31

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40
    I know. I am not very clever. I also don't write program with VB ago. But I can't accept anyone who abuse me.
    even my english is very bad. So I don't know how to say and you can understand me.

  32. #32
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    The chm file could got corrupted... if that's so, there's no code in the world that would open the file. That's why I ask you if you can open it by double-clicking... I was checking that the file is not corrupted and that you have all the components needed installed on your machine to open that kind of files.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  33. #33
    New Member
    Join Date
    Jul 2002
    Location
    Delaware
    Posts
    4
    I used
    ----------------------------------------------------------------------------
    Private Declare Function ShellExecute Lib "shell32.dll" Alias _
    "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory _
    As String, ByVal nShowCmd As Long) As Long
    Private Const SW_SHOWNORMAL = 1

    Private Sub Command1_Click()
    ShellExecute 0&, vbNullString, _
    "d:\223\234.chm", _
    vbNullString, "C:\", SW_SHOWNORMAL
    End Sub
    ---------------------------------------------------------------------------
    and got it working. Here's my question now. What if I know the name of the file I want to open, but I don't know its exact path. I want to open a file that is in the same directory as the executable. I know the files name, how do tell it to look in the same directory.

    Thanks,
    Steve

  34. #34

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40
    I can open that kind of file by double clicking.
    It's ordinary file. It's only a help file like .hlp

  35. #35
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Use
    VB Code:
    1. App.Path
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  36. #36
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by flyflydream2002
    I can open that kind of file by double clicking.
    It's ordinary file. It's only a help file like .hlp
    I know what kind of file they are... but they can get corrupted anyway.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  37. #37

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40
    Yes.I can open it.
    Thank u.

  38. #38
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by flyflydream2002
    Yes.I can open it.
    Thank u.
    Then, re-check the code I gave you. As you can see, Sr2003 got it working.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  39. #39

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    40
    Thank everyone.
    Thank Mc Brain

  40. #40
    New Member
    Join Date
    Jul 2002
    Location
    Delaware
    Posts
    4
    app.path


    Can you give me a quick example. I am trying to brush off the rust from my VB skills, been digging through quake 3 code, and its a little different

    Thanks,
    Steve

Page 1 of 2 12 LastLast

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