Results 1 to 13 of 13

Thread: Opening a text file from common dialog with a Text box.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Posts
    98
    Could someone tell me why I get
    'Variable Required - cant assign to this expression'
    when I try to use this code from a command button.

    Private Sub Command1_Click()
    With CommonDialog
    .ShowOpen
    .Filter = "Text File|*.txt|"
    End With
    Open CommonDialog.filename For Input As #1
    Do Until EOF(1) = True
    Get #1, , Text1
    Loop
    Close #1
    End Sub

    ?????
    Visual basic 6.0 Enterprise Edition
    [email protected]

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Which line does VB highlight / pick out
    Did you dim the Text1 variable ?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Posts
    98
    It highlights

    Get #1, , Text1
    Visual basic 6.0 Enterprise Edition
    [email protected]

  4. #4
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    What is Text1.....a variable or a textfield????
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Posts
    98
    A text field, which I want the text from cd.filename to be loaded into.
    Visual basic 6.0 Enterprise Edition
    [email protected]

  6. #6
    Hyperactive Member
    Join Date
    Apr 2001
    Posts
    315
    If text1 is a textbox then you supplied an object (forget about default propert TEXT here).
    Dim strText as string
    Get #1, , strText
    text1.text = strText

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Posts
    98
    I dont think I can do it.. I always get Bad file Mode. =\
    Visual basic 6.0 Enterprise Edition
    [email protected]

  8. #8
    Hyperactive Member
    Join Date
    Apr 2001
    Posts
    315
    Start getting used to using FileSystemObject. Open , Get and Put etc are going away in VB.Net in favor of objects.
    text1.text = ReturnFileasString(CommonDialog.filename)
    Code:
    '* Public in Form, Standard Module or method of Class 
    Public Function ReturnFileAsString(byval strPathFile as string) as string
        Dim objFS as object
        Dim objTs as object
        Dim strText as string
        Set objFS = CreateObject("scripting.FileSystemObject")
        if not objFs.FileExsts(strPathFile) then exit sub
    '* Need ON ERROR to catch ACCESS DENIED etc
        Set objTS = objFS.OpenTextFile(strPathFile, ForReading, False, False)
        objTS.ReadAll strText
        objts.close
        Set objTs = nothing
        Set objFS = nothing
        ReturnFileAsString = strText
    End Sub
    Last edited by John Yingling; Apr 22nd, 2001 at 06:04 PM.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Posts
    98
    that doesnt seem to work either, you make that a public sub in a bas file, am I right ?
    Visual basic 6.0 Enterprise Edition
    [email protected]

  10. #10
    Hyperactive Member
    Join Date
    Apr 2001
    Posts
    315
    PUBLIC Sub wherever, Form, Standard Module, Class.
    What is the error message. "Doesn't work" is sort of nebulous.
    "can't create component"
    "file not found"
    "object does not support..."
    etc.

  11. #11
    Hyperactive Member
    Join Date
    Apr 2001
    Posts
    315
    My fault, But you should have been able to fix it from the syntax error.
    Use FUNCTION, not SUB.

  12. #12
    Matthew Gates
    Guest
    Try this:


    Code:
    Private Sub Command1_Click()
    
        Dim iFile As Integer
        On Error GoTo User_Cancelled
        With CommonDialog1
            .CancelError = True
            .DialogTitle = "Select a Text File.."
            .Filter = "Text File (*.txt)|*.txt"
            .ShowOpen
            iFile = FreeFile
            Open .filename For Input As iFile
                Text1 = Input(LOF(iFile), iFile)
            Close iFile
        End With
    User_Cancelled:
    
    End Sub

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Posts
    98
    YES! thats works ... thanks !
    Visual basic 6.0 Enterprise Edition
    [email protected]

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