Running an Access macro from a VB.NETprogram

S

Shooter4Life8

I am trying to run a macro from my VB.NET program. Here is my code.

Dim myAccess As Access.Application
Dim allMacro As String = "ALL-Macros"
myAccess.DoCmd.RunMacro(allMacro)

I get the error.

"An unhandled exception of type 'System.NullReferenceException'
occurred in
WindowsApplication1.exe

Additional information: Object reference not set to an instance of an
object."

I am new to running other programs through a program of my own and am
not sure what I am doing wrong. Any help or pointers on the subject are
welcome!

Thanks!
 
Z

zacks

I am trying to run a macro from my VB.NET program. Here is my code.

Dim myAccess As Access.Application
Dim allMacro As String = "ALL-Macros"
myAccess.DoCmd.RunMacro(allMacro)

I get the error.

"An unhandled exception of type 'System.NullReferenceException'
occurred in
WindowsApplication1.exe

Additional information: Object reference not set to an instance of an
object."

I think you will need to do a CreateObject call for the myAccess
object.

Do a search of this newsgroup for Excel.Application (which undoubtedly
works in a similar fasion) for some insight.
 
S

Shooter4Life8

I tried adding

myAccess = CreateObject("Access.Application")

just before my DoCmd.RunMacro command and now I am recieving the error
message

"An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in
WindowsApplication1.exe

Additional information: You can't carry out this action at the present
time."

do i need to start somthing else before i can call this?
 
S

Shooter4Life8

I added some more and am getting a strange error.

Dim myAccess As Access.Application
Dim allMacro As String = "ALL-Macros"

myAccess = CreateObject("Access.Application")
myAccess.Visible = True
myAccess.OpenAccessProject("c:\data\idCard.mdb")
myAccess.DoCmd.RunMacro(allMacro)

the error is

"An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in
WindowsApplication1.exe

Additional information: Microsoft Office Access can't open the database
because it is missing, or opened exclusively by another user."

I know my path is correct, not very sure how to proceed.

thanks again
 
H

HKSHK

Dear Shooter4Life8,
Additional information: Microsoft Office Access can't open the database
because it is missing, or opened exclusively by another user."

If you use Access with that database at the same time it might be, that
Access opened it exclusively. In this case, you would have to close
Access first before your program can open the database.

If this is not the case, check if there is an LDB file (when neither
Access nor your program is running. If that is the case, delete it and
try again.

Best Regards,

HKSHK
 
S

Shooter4Life8

I have tried closing all instances of the Access program before I run
my program. My program opens Access to try to do this.. Could that be
part of the problem?
Not sure what an LDB file file is.
 
M

Mark

An ldb file is an Access locking file (located in the same folder as your
mdb, mde or mdw)... You will only see it if someone (or yourself) has the
database open (even if via linked tables etc.). If your Access mdb is
secured using Workgroup security you will also have to reference the
appropriate mdw (i.e. workgroup file) to open it.

I'm not sure if an ldb would be created against the original file if you use
createOnject. I have never had to do this myslef.

You could also try opening the ldb in notepad (you may have to wrap the
text) and you will see the machine and user name for anyone accessing the
mdb. You could possibly be in there twice. Note that the ldb sometimes still
shows users that have logged out already (though I don't think this would
cause you a problem).

Try:
a) Before running your code, check if there is an ldb accompanying your mdb
(i.e. in the same folder), if there is then someone has the mdb open (or the
ldb hasn't been cleared so just delete it, or you have a process running
thats still accessing it).

b) Run to the error, without clicking 'OK' navigate to wherever the mdb is
and check for an ldb, if there is one present now that wasn't before, have a
look inside and see who's logged in (you may be in there more than once.)

c) Click OK, see if it still exists after passing the error.

This is not a solution, but may help you pin down the problem...

Also (and I may be talking complete rubbish here), open up Acces, go to
options and see if it is set to open mdb's exclusively by default. I'm not
sure if this would affect "createObject" but if it did then it may be worth
deselecting the option and trying again.

Note: If all you are doing is manipulating data then you'd be better off
just migrating the macro into vb...

hth Mark
 
S

Shooter4Life8

The LDB file gets created when I open the file just as you said and
goes away when I close it so I am the only one accessing the database.
When I start access and try to open the file however, it does not
create a ldb file. In fact, the database I try to open (along with the
ldb file) never gets opened before during or after the error.

I also checked the options and the default open mode was shared instead
of exclusive (which doesnt make much sense with the error message im
getting).

Migrating the macro into vb? I looked at the macro to see what it is
doing but am unsure how to implement it into VB... I am very new to
this sort of programming and learning a lot as I go, thanks for all the
help I really appreciate it.
 
P

Paul Clement

On 9 Oct 2006 07:53:18 -0700, (e-mail address removed) wrote:

¤ The LDB file gets created when I open the file just as you said and
¤ goes away when I close it so I am the only one accessing the database.
¤ When I start access and try to open the file however, it does not
¤ create a ldb file. In fact, the database I try to open (along with the
¤ ldb file) never gets opened before during or after the error.
¤
¤ I also checked the options and the default open mode was shared instead
¤ of exclusive (which doesnt make much sense with the error message im
¤ getting).
¤
¤ Migrating the macro into vb? I looked at the macro to see what it is
¤ doing but am unsure how to implement it into VB... I am very new to
¤ this sort of programming and learning a lot as I go, thanks for all the
¤ help I really appreciate it.
¤

Is the read-only attribute set on the database file by any chance? Is the database located on a
network resource?


Paul
~~~~
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top