CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jul 2003
    Posts
    52

    Problems while Loading a file.

    I am having a weird problem in my application. the problem occurs when I am opening an existing database file on my application. If I press Cancel on the Open Exisiting file dialog box, the application defaultly opens the last database it opened instead of just doing nothing. If you do selected ok to open a database, the application goes through and opens it..which is fine..How do I fix this problem. Here is the relevant code for File Load

    Private Sub FileLoad_Click()

    CD.Flags = cdlOFNFileMustExist
    On Error GoTo fokop
    CellText.Visible = False
    CD.DialogTitle = "Load existing analysis..."
    CD.Filter = "MS Access Database Files (*.mdb)|*.mdb|All Files (*.*)|*.*"
    CD.ShowOpen


    GlobalDatabaseName = CD.FileName

    If GlobalDatabaseName = "" Then
    Exit Sub
    End If


    Dim MyDB As Database, thesql As String, theset As Recordset

    Set MyDB = OpenDatabase(GlobalDatabaseName)

    thesql = "SELECT * FROM HeaderData"
    Set theset = MyDB.OpenRecordset(thesql)
    If theset.EOF = False Then
    theset.MoveFirst
    Text21.Text = Replace(FixNull(theset.Fields(0).Value), "@@@", "")
    Text22.Text = Replace(FixNull(theset.Fields(1).Value), "@@@", "")
    Text23.Text = Replace(FixNull(theset.Fields(2).Value), "@@@", "")
    DTPicker1.Value = FixNull(theset.Fields(3).Value)
    DTPicker1.Value = FixNull(theset.Fields(4).Value)
    Text24.Text = Replace(FixNull(theset.Fields(5).Value), "@@@", "")
    Text25.Text = Replace(FixNull(theset.Fields(6).Value), "@@@", "")
    Text26.Text = Replace(FixNull(theset.Fields(7).Value), "@@@", "")
    Text1.Text = Replace(FixNull(theset.Fields(8).Value), "@@@", "")
    End If
    theset.Close
    thesql = "SELECT AssemblyName FROM SparesData GROUP BY [AssemblyName]"
    Set theset = MyDB.OpenRecordset(thesql)
    If theset.EOF = False Then
    Combo1.Clear
    theset.MoveFirst
    Do While theset.EOF = False
    Combo1.AddItem theset.Fields(0).Value
    theset.MoveNext
    Loop
    Combo1.Text = Combo1.List(0)
    End If
    theset.Close

    thesql = "SELECT * FROM AnalysisTeam"
    Set theset = MyDB.OpenRecordset(thesql)
    If theset.EOF = False Then
    theset.MoveFirst
    k = 0
    Do While theset.EOF = False
    k = k + 1
    FGATeam.TextMatrix(k, 0) = Replace(theset.Fields(0).Value, "@@@", "")
    FGATeam.TextMatrix(k, 1) = Replace(theset.Fields(1).Value, "@@@", "")
    FGATeam.TextMatrix(k, 2) = Replace(theset.Fields(2).Value, "@@@", "")
    theset.MoveNext
    Loop
    End If
    theset.Close

    Frame2.Visible = True

    SparesMain.Caption = "Spares Definition (" & GlobalDatabaseName & ")"
    Call LoadTreeFromAccess
    Call FixTreeText
    StatusBar1.Panels(2).Text = "File loaded successfully."

    Exit Sub

    fokop:
    StatusBar1.Panels(2).Text = "Unable to perform operation."
    Call FileExport_Click

    End Sub

  2. #2
    Join Date
    Aug 2003
    Posts
    8
    Common Dialog has a property CancelError for cancel button pressing.

    You should check for that property true or false.

    write the following after cdl.showopen

    If cdl.CancelError = false Then
    'Write corresponding code here
    Exit sub
    end if
    Last edited by cutamacious; August 8th, 2003 at 05:59 AM.
    Cute Member

  3. #3
    Join Date
    Jul 2003
    Posts
    52
    Thanks but its not working..what do u mean by write corresponding code here??

  4. #4
    Join Date
    Jul 2003
    Posts
    52
    On pressing Cancel on the Load File..it opens the last database it opened.... i dont seem to understand why this is happening..thanks for ur help also

  5. #5
    Join Date
    Aug 2003
    Posts
    8
    Originally posted by cutamacious
    Common Dialog has a property CancelError for cancel button pressing.

    You should check for that property true or false.

    write the following after cdl.showopen

    If cdl.CancelError = false Then
    'Write corresponding code here
    Exit sub
    end if

    Write Corresponding code here refers to any cleanup code u wish to put there like for ex., displaying a msgbox or on status bar etc.,
    Cute Member

  6. #6
    Join Date
    Aug 2003
    Posts
    8
    Originally posted by azwildcat4ever
    On pressing Cancel on the Load File..it opens the last database it opened.... i dont seem to understand why this is happening..thanks for ur help also

    well commondialog happens to remember the previous file name it opened. So use the cancelerror property to stop the code following the cdl.showopen statement
    Cute Member

  7. #7
    Join Date
    Jul 2003
    Posts
    52
    I tried this it worked and did nothing when I click Cancel however now it doesnt do anything when trying to open a database.

  8. #8
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    275
    Hope this helps!
    Code:
    Private Sub Form_Load()
        On Error Resume Next
        CD.CancelError = True
        CD.ShowOpen
        If Err.Number = 32755 Then
            MsgBox "Cancel was selected"
        Else
            MsgBox CD.FileName
        End If
    End Sub

  9. #9
    Join Date
    Jul 2003
    Posts
    52
    WEll I just entered CD.CancelError = True after CD.Showopen and it worked fine..weird ???

  10. #10
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    275
    Oh!
    I think it should only work before ShowOpen.

  11. #11
    Join Date
    Aug 2003
    Posts
    8
    Originally posted by azwildcat4ever
    WEll I just entered CD.CancelError = True after CD.Showopen and it worked fine..weird ???
    Well, you force the commodialog to raise an error by giveing that statement
    Cute Member

  12. #12
    Join Date
    Jul 2003
    Posts
    52
    so what sohuld i do..where does wht code?c

  13. #13
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Well, just change this part of the code so that it will work..
    Code:
    CD.Flags = cdlOFNFileMustExist
    On Error GoTo fokop
    CellText.Visible = False
    CD.DialogTitle = "Load existing analysis..."
    CD.Filter = "MS Access Database Files (*.mdb)|*.mdb|All Files (*.*)|*.*"
    CD.CancelError = True
    CD.ShowOpen
    Busy

  14. #14
    Join Date
    Jul 2003
    Posts
    52
    woked thanks alot

  15. #15
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    275
    Code:
    Private Sub FileLoad_Click()
    on error resume next
    CD.Flags = cdlOFNFileMustExist
    CellText.Visible = False
    CD.DialogTitle = "Load existing analysis..."
    CD.Filter = "MS Access Database Files (*.mdb)|*.mdb|All Files (*.*)|*.*"
    CD.CancelError = true
    CD.ShowOpen
    If Err.Number = 32755 Then
        goto Canceled    
    End If
    
    
    On Error GoTo fokop
    
    GlobalDatabaseName = CD.FileName
    
    If GlobalDatabaseName = "" Then
    Exit Sub
    End If
    
    
    Dim MyDB As Database, thesql As String, theset As Recordset
    
    Set MyDB = OpenDatabase(GlobalDatabaseName)
    
    thesql = "SELECT * FROM HeaderData"
    Set theset = MyDB.OpenRecordset(thesql)
    If theset.EOF = False Then
    theset.MoveFirst
    Text21.Text = Replace(FixNull(theset.Fields(0).Value), "@@@", "")
    Text22.Text = Replace(FixNull(theset.Fields(1).Value), "@@@", "")
    Text23.Text = Replace(FixNull(theset.Fields(2).Value), "@@@", "")
    DTPicker1.Value = FixNull(theset.Fields(3).Value)
    DTPicker1.Value = FixNull(theset.Fields(4).Value)
    Text24.Text = Replace(FixNull(theset.Fields(5).Value), "@@@", "")
    Text25.Text = Replace(FixNull(theset.Fields(6).Value), "@@@", "")
    Text26.Text = Replace(FixNull(theset.Fields(7).Value), "@@@", "")
    Text1.Text = Replace(FixNull(theset.Fields(8).Value), "@@@", "")
    End If
    theset.Close
    thesql = "SELECT AssemblyName FROM SparesData GROUP BY [AssemblyName]"
    Set theset = MyDB.OpenRecordset(thesql)
    If theset.EOF = False Then
    Combo1.Clear
    theset.MoveFirst
    Do While theset.EOF = False
    Combo1.AddItem theset.Fields(0).Value
    theset.MoveNext
    Loop
    Combo1.Text = Combo1.List(0)
    End If
    theset.Close
    
    thesql = "SELECT * FROM AnalysisTeam"
    Set theset = MyDB.OpenRecordset(thesql)
    If theset.EOF = False Then
    theset.MoveFirst
    k = 0
    Do While theset.EOF = False
    k = k + 1
    FGATeam.TextMatrix(k, 0) = Replace(theset.Fields(0).Value, "@@@", "")
    FGATeam.TextMatrix(k, 1) = Replace(theset.Fields(1).Value, "@@@", "")
    FGATeam.TextMatrix(k, 2) = Replace(theset.Fields(2).Value, "@@@", "")
    theset.MoveNext
    Loop
    End If
    theset.Close
    
    Frame2.Visible = True
    
    SparesMain.Caption = "Spares Definition (" & GlobalDatabaseName & ")"
    Call LoadTreeFromAccess
    Call FixTreeText
    StatusBar1.Panels(2).Text = "File loaded successfully."
    
    Exit Sub
    
    Canceled:
       msgbox "Canceled!"
       exit sub
    fokop:
    StatusBar1.Panels(2).Text = "Unable to perform operation."
    Call FileExport_Click
    
    End Sub

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

Featured