Search code examples
ms-accessvbscriptbartender

Is it possible to put vba code behind BarTender Design and have it run a delete query in access


  1.Public sub test()
  2.dim cn, rs, cmd
  3.Set cn = createobject("ADODB.Connection")
  4.Set rs = createobject("ADODB.Recordset")
  5.Set cmd = createobject("ADODB.Command")
  6.connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;
DataSource=C:\Users\text\Documents\PrintCenterForm\PrintCernter_v1.accdb;"
  7.cn.Open connectionString
  8.cmd.ActiveConnection = cn
  9.cmd.Execute
  10.End sub

Sorry Guys, I posted a picture of my code and error yesterday but for some odd reason they deleted it?

But anyways I am getting an error On Line 8 Stating:"Command text was not set for the command object."

I have also tried rewriting this code as it is doing no good just sitting here waiting for a response but I have also tried this way too but cannot seem to get this to work

 1.Set accessApp = GetObject("C:\Users\texthere\Documents\PrintCenterForm\PrintCernter_v1.accdb")
 3.Set accessApp = createObject("Access.Applicaiton")
 4.accessApp.visible = true
 5.accessApp.UserControl = true 
 6.accessApp.OpenCurrentDataBase
("C:\Users\texthere\Documents\PrintCenterForm\PrintCernter_v 
 1.accdb"),false
 7.accessApp.Run "Qry_DeletePrinted"
 8.End Sub

I am getting a error on line 1 on the bottom code stating: "ActiveX component can't create object: 'Access.Application' I am more than likely not going to use this code unless you guys see that it would be easier to work with? I also, changed the OnPrintJobStart to OnNewRecord to see if this approach work but first I need to come through the coding issue.

My end result I would just like some code that will kick off a delete query in Access to delete records whenever they are printed from Bartender.

I have a delete query already in Access call "Qry_DeletePrinted".

This database is located on my C:Drive DataSource=C:\Users\text\Documents\PrintCenterForm\PrintCernter_v1.accdb

Just to clarify, I am using MS Access 2013

As you both can see I am nowhere near familiar to VB Script


Solution

  • BELOW IS WHAT THESE GUYS HELPED ME ACCOMPLISH AND IT WORKS LIKE A CHARM!

     Dim cn, rs, cmd
       set cn = CreateObject("ADODB.Connection")
       set rs = CreateObject("ADODB.Recordset")
       set cmd = CreateObject("ADODB.Command")
       ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data 
       Source=\\NetworkDriveName\PrintCernter_v1.accdb;"
       cn.Open ConnectionString 
       sql = "Qry_DeletePrinted" 
       '+ Format.NamedSubStrings("Printed_User").Value
       cmd.ActiveConnection = cn
       cmd.CommandText = sql
       cmd.execute
       cn.close