|
-
Sep 30th, 2004, 12:52 PM
#1
Thread Starter
New Member
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?
-
Sep 30th, 2004, 01:04 PM
#2
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:
Private Sub Command1_Click()
Dim AccessApp As Access.Application
Set AccessApp = New Access.Application
AccessApp.OpenCurrentDatabase ("C:\music.mdb")
AccessApp.Visible = True
AccessApp.DoCmd.OpenForm "frmmusic"
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 1st, 2004, 03:50 AM
#3
Thread Starter
New Member
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.
-
Oct 1st, 2004, 07:56 AM
#4
Hyperactive Member
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
-
Oct 1st, 2004, 08:48 AM
#5
Thread Starter
New Member
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.
-
Oct 1st, 2004, 10:03 AM
#6
Hyperactive Member
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
-
Oct 1st, 2004, 10:09 AM
#7
Lively Member
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.
-
Oct 1st, 2004, 10:51 AM
#8
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:
Option Explicit
Private Sub Command1_Click()
Dim AccessApp As Access.Application
Set AccessApp = New Access.Application
AccessApp.Visible = True
'Make sure you set the macros warning to low just for testing.
'Then you can open the db with no problems.
AccessApp.OpenCurrentDatabase ("D:\development\music.mdb")
AccessApp.DoCmd.OpenForm "frmMusic", acNormal, , , acFormEdit, acWindowNormal
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 1st, 2004, 12:43 PM
#9
Hyperactive Member
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:
Const Vision_Path = "E:\Vision.MDB"
Dim accApp As Access.Application
Private Sub cmdVisionOpenForm_Click()
Set accApp = New Access.Application
accApp.OpenCurrentDatabase (Vision_Path)
accApp.Visible = True
accApp.DoCmd.OpenForm "Vision_Lookup"
Set accApp = Nothing
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
-
Oct 1st, 2004, 01:50 PM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Oct 1st, 2004, 03:00 PM
#11
Lively Member
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:
Option Explicit
Private Sub Command1_Click()
Dim AccessApp As Access.Application
Set AccessApp = New Access.Application
AccessApp.Visible = True
'Make sure you set the macros warning to low just for testing.
'Then you can open the db with no problems.
AccessApp.OpenCurrentDatabase ("D:\development\music.mdb")
AccessApp.DoCmd.OpenForm "frmMusic", acNormal, , , acFormEdit, acWindowNormal
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|