MS Access VBA Print Statement Call Bug

I am having a small bug with some code I wrote and I did not notice at first. I am unsure how to describe it. The code is to open a cash register drawer using VBA. For some reason the code on click works excellent the first time used but it does not work great if I close the drawer and try again. If I wait a while and try again later or if I close access and reopen access it works again.

I even wrote a article to help others out and you can see it here: https://www.experts-exchange.com/articles/31507/MS-Access-How-To-Control-and-Open-a-POS-Cash-Register-Cash-Drawer-With-VBA.html

Anyway my code is:
Private Sub OpenCashDrawer()
On Error GoTo Errorhandler
Dim LocationPort As String

LocationPort = "Com4" 'Replace Com4 with the com port you are using. Example is Com3, Com2, etc.....

Open LocationPort & ":9600,n,8,1" For Output As #1
Print #1, "" 'This is just sending a signal like it is printing something but it is not.
Close #1

Exit Sub
Errorhandler:
MsgBox "Error " & Err.Number & ": " & Err.Description & " in " & _
VBE.ActiveCodePane.CodeModule, vbCritical, "Error in btnOpenCashDrawer_Click"
End Sub

Open in new window


I believe it has something to do with the print call and it is (Not Really) printing something and I have to wait. Does anyone have any ideas.....

Thanks for the help.
LVL 1
Dustin StanleyEntrepreneurAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PatHartmanCommented:
For some reason the code on click works excellent the first time used but it does not work great if I close the drawer and try again
Add a breakboint in the procedure.  Then see what happens the second time you press the button.

You might need to include a
DoEvents
At the end of the procedure to wake Windows up to disconnect the link to the drawer.
0
Dustin StanleyEntrepreneurAuthor Commented:
Thanks Pat but neither worked. This is just curiosity and it may be completely on the other end of the spectrum but......

is it possible the PC itself is not Electrically sending enough power to the solenoid to release the drawer? Maybe something like the PC is recharging to gain power......Make sense???  I know that is more mechanical than Code but just a thought.

But it doesn't make sense when I can close access and reopen it and then it work right away.
0
PatHartmanCommented:
I don't think so.  It sounded more to me like closing the object didn't actually release it.  I sometimes have trouble when I try to use spreadsheets that others might open when I am using them.  I don't know if there is an option to set #1 to empty or null but that might do it if one of those works.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Gustav BrockCIOCommented:
You may have to print "something". This has always worked for me:

  Dim strPort as String
  Dim strOpenDrawer as String

  strPort = "1"     ' Adjust for 1, 2 or 3 as needed.
  strPort = "LPT" & strPort  ' or COM port.
  strOpenDrawer = Chr(130)

  ' Send special character string to the cash drawer via PC port.
  Open strPort For Output As #1
  Print #1, strOpenDrawer
  Close #1

Open in new window

0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
I'd be very surprised if printing a empty string is the command for opening the drawer.  Usually it's a fairly long escape sequence, although you are accessing it via a com port rather than as a printer.

 What type of POS is it?

Jim.
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
<< What type of POS is it?>>

 I just went through your article and see the setup now.

 I think more than anything, your problem lies with the trigger.    I do see that it says "any character" can be sent to the trigger to open the cash drawer.

  So try sending something rather than an empty string.

Jim.
0
Dustin StanleyEntrepreneurAuthor Commented:
Jim I tried Printing Hello:
Print #1, "Hello"

Open in new window

But the same effect. I have calculated it to be a 7 Second wait in between the When i click it and it works and then when I can click it again and it works fine again. I know in the real world this may not be a problem but I don't want the code to fail when needed.

Gustav I have not had the time yet to try your solution. I will let you know when i have done it. Thanks.
0
PatHartmanCommented:
Was the time delay something you set on the device or something you did in the code?
0
Dustin StanleyEntrepreneurAuthor Commented:
Pat
Was the time delay something you set on the device or something you did in the code?
Nope. I plugged in the device and inserted that VBA code. That was it. You no when you print something and it takes a little while to pop up a display saying xxxPages then off to the printer and it starts printing and etc..... I believe this is called spooling maybe. I am not sure. But anyways I thought maybe this is the delay.... I would like to be just 100% confident in the code before using it. Just wouldn't be very professional when I checkout a guest and my register won't open.
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
<< I believe this is called spooling maybe.>>

  Yes, but that deals with printers.  Since your addressing this to a com port, that doesn't come into play.  The only things involved here is your code, the driver code for the trigger, and the drawer.

   To check it's not your code, you can issue the following command at a command prompt in windows:

echo hello>comX

replacing X with the port number.   This should open the drawer.   Try that and see if there is a delay.   If you want, you can even put that in a batch file and then execute the batch file.   If you want to put it in a shortcut, right click on the desktop, do "New shortcut" and then put this in for the location:


"C:\Windows\System32\cmd.exe /c echo hello>comX

If there is no delay, then somehow it's your code.   If the delay is still there, then I would  go back and look at the drawer documentation since the trigger has no real docs.   Is there any kind of a delay there?    Just so you understand, the PC only sends a signal to the drawer to open, it doesn't actually power a solenoid to do the job itself.   So the delay may be in the drawer.  

 If you find nothing there, then I would poke around in device manager and look at the driver properties.  There maybe something there.   I tried to install the driver here to look at it, but without the trigger connected, the driver doesn't show.

Jim.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dustin StanleyEntrepreneurAuthor Commented:
Thanks for the help. Gustav same effect and Jim the batch file has the same delay effect as well. I will dig out the documentation and see what it says.

But Jim you said
so you understand, the PC only sends a signal to the drawer to open, it doesn't actually power a solenoid to do the job itself.

This drawer has no power supply and neither does the usb trigger adapter. So what powers the solenoid? It has to be coming from the USB port.....  The drawer requires 12V DC.
0
Gustav BrockCIOCommented:
This drawer has no power supply
The drawer requires 12V DC.

These statements exclude each other. The last has to be true.
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Sorry, I should have been clearer.  It's the trigger box that is supplying the drawer (which normally is done with a receipt printer otherwise), and it is getting that from the USB if it doesn't have any external power itself.

 But on the com port, your just transmitting some data.

<< Jim the batch file has the same delay effect as well.>>

  That means the delay is in the drawer (if it has any smarts), or the trigger.  If it's just a drawer with a solenoid to open the drawer, then it's the trigger for sure.

Jim.
0
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
The delay, and the fact that it resumes "normal" operation suggest some sort of time out to me. Perhaps it is waiting for something?

What if you send chr(13)+chr(10) in the print statement? Those are often used in other applications, e.g. barcode scanners communicating through ports.

Print #1, chr(13)+chr(10)

Open in new window

0
Dustin StanleyEntrepreneurAuthor Commented:
Anders Ebro:
Same thing. Not working.

Jim I agree. I think it is something with the trigger USB device. The cash register is just a drawer with a solenoid. I believe (Wherever it is at) it is a power issue or something of the like.
Scenario:

Click btnOpenDrawer = Drawer opens fine
2 Seconds later Click btnOpenDrawer again = Kinda hear a very light knock on the drawer from the solenoid.

4-5 Seconds later Click btnOpenDrawer again = Hear a definite knock on the drawer from the solenoid but still does move or open.

7 Seconds later Click btnOpenDrawer = The drawer opens fine

I have played around with the driver options but I am not all the experienced with it to know what I am doing. But nothing changed the effect.

Maybe the trigger has capacitors or something in it that has to refill for power....
0
Gustav BrockCIOCommented:
Maybe the trigger has capacitors or something in it that has to refill for power....

Sounds more likely. Try using a power supply that can deliver a higher surge current. A car battery will do (for testing, of course).
0
Dustin StanleyEntrepreneurAuthor Commented:
Gustav Brock ;) your now trying to get me to tear into my device.....as much as I love tinkering I will have to pass. I kinda need it.  But if your talking about just using a battery to to open it then yes it works fine that way. I have a variable DC Power supply that I set to 12VDC and it kicks the drawer as soon as I say every time guaranteed.
0
Gustav BrockCIOCommented:
Well, if it seems to be a simple powering issue, then this tends to be a discussion between you and the supplier of the drawer.
0
Dustin StanleyEntrepreneurAuthor Commented:
I have done everything possible and it has came down to a 90% chance of it being a powering issue in the trigger. Basically the trigger needs to recharge before being able to get enough charge to kick the drawer again immediately. Thank you all for the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Printers and Scanners

From novice to tech pro — start learning today.