Results 1 to 11 of 11

Thread: Visual Basic 6.0 and Access

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    7

    Question Visual Basic 6.0 and Access

    Hi,

    I seem to be having problems with this code below, I have a Form made using Visual Basic 6.0 and a command button, inside the command button is:


    Code:
    Dim AccessApp As New Access.Application 
    Set AccessApp = CreateObject ("Access.Application") AccessApp.OpenCurrentDatabase ("C:\music.mdb") AccessApp.Visible = True AccessApp.DoCmd.OpenForm "frmmusic"
    Now, what I'm trying to do is open a current database from this one command button, the database is stored on C:\ called music.mdb this seems to work but the problem i seem to be having is once Microsoft Access opens.. it closes straight away, any suggestions on how I can resolve this problem?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Is this the way you actual code is - all on one line? Your first
    line has the dim and new keywords so it is creating a new access
    application there. Then you set it to createobject to another
    access application. You dont use both.

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim AccessApp As Access.Application
    4.  
    5.     Set AccessApp = New Access.Application  
    6.     AccessApp.OpenCurrentDatabase ("C:\music.mdb")  
    7.     AccessApp.Visible = True
    8.     AccessApp.DoCmd.OpenForm "frmmusic"
    9.  
    10. End Sub
    Something like this show fix it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    7
    Hi,

    No, The code isn't all on one line, and the fix you gave doesn't seem to fix the problem, it opens MS Access and closes instantly, are there any fixes to keep Access open for the user?

    this is basically the same as what the code i posted above is doing, help in fixing this problem will be greatly appreciated. thx.
    Last edited by tariq123; Oct 1st, 2004 at 05:04 AM.

  4. #4
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    See Tools - Startup options

    Are you doing either or both of these:
    AccessApp.CloseCurrentDatabase
    Set AccessApp = Nothing

    I'm having a similar problem, but it seems to relate to whether or not my Access DB starts up with the switchboard form and/or the database window.

    If it starts up with both of these already, and I'm opening a separate form (not the switchboard) from the VB code
    I can still do the Set AccessApp = Nothing and everything works fine. When closing the form itself, then all of Access goes away.

    It's all magic. I don't understand it.
    In my case I wouldn't be surprised if something was corrupted.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Location
    London
    Posts
    7
    Hi,

    I'm using none of them, what I am trying to do is open up a current database stored on C:\ just by using a command button. I have succeeded in doing so, but the problem is keeping it open, once the database is open it just closes instantly. if there is a fix for the code above then that would be great if someone could also point out what seems to be going wrong.

  6. #6
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Startup form?

    Try making a dummy form or switchboard and
    in Tools - Startup... put that in Display Form/Page
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  7. #7
    Lively Member
    Join Date
    Sep 2004
    Posts
    96
    Move your dim statement.
    When the sub is done, the variable falls out of scope and releases. Since you then no longer have a valid handle on Access, it thinks you are done and closes.

    It's as simple as that.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    No, its not as simple as that. The code works on Access 2003 just
    fine even though the variable goes out of scope. It is not invoking
    the accApp.Quit or Set accApp = Nothing so it should stay open
    depending it its not version specific.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.  
    5.     Dim AccessApp As Access.Application
    6.  
    7.     Set AccessApp = New Access.Application
    8.     AccessApp.Visible = True
    9.     'Make sure you set the macros warning to low just for testing.
    10.     'Then you can open the db with no problems.
    11.     AccessApp.OpenCurrentDatabase ("D:\development\music.mdb")
    12.     AccessApp.DoCmd.OpenForm "frmMusic", acNormal, , , acFormEdit, acWindowNormal
    13.  
    14. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Startup form

    Below is code I'm working with right now.
    With my switchboard form set as Startup it works fine.
    When I set Startup form = none it flashes and dissappears.

    VB Code:
    1. Const Vision_Path = "E:\Vision.MDB"
    2. Dim accApp As Access.Application
    3.  
    4. Private Sub cmdVisionOpenForm_Click()
    5.     Set accApp = New Access.Application
    6.  
    7.     accApp.OpenCurrentDatabase (Vision_Path)
    8.     accApp.Visible = True
    9.     accApp.DoCmd.OpenForm "Vision_Lookup"
    10.  
    11.     Set accApp = Nothing
    12. End Sub

    Do you have a startup form set in your Access DB?
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    No just a blank db with one empty form. Do you do a step by
    step execution of code (F8)? Also, are your macros set to what
    level? If they are not enabled the db may not open and if you
    have no error trapping then it will close.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    Lively Member
    Join Date
    Sep 2004
    Posts
    96
    Originally posted by RobDog888
    No, its not as simple as that. The code works on Access 2003 just
    fine even though the variable goes out of scope. It is not invoking
    the accApp.Quit or Set accApp = Nothing so it should stay open
    depending it its not version specific.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.  
    5.     Dim AccessApp As Access.Application
    6.  
    7.     Set AccessApp = New Access.Application
    8.     AccessApp.Visible = True
    9.     'Make sure you set the macros warning to low just for testing.
    10.     'Then you can open the db with no problems.
    11.     AccessApp.OpenCurrentDatabase ("D:\development\music.mdb")
    12.     AccessApp.DoCmd.OpenForm "frmMusic", acNormal, , , acFormEdit, acWindowNormal
    13.  
    14. End Sub
    Ok, given your code, I buy that, given the original code which used a CreateObject..... not so sure, still think the variable falling out of scope would kill it.

    But anyways.....

    Do you need to control the DB or access it? Or are you just trying to open the Access file? Would a ShellExecute on the mdb work in this case? If no automation is needed and all you want is the database open for the user to use, I don't see why that wouldn't work.

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