Empty Print Queue Using VBA - ms-access

I am using MS Access 2019
I want to empty the print queue (Using VBA) for a specific printer...if this cannot be done just empty all print jobs on all printers.
I am creating a POS using Access 2019. At one point the program ask if customer wants a receipt. IF the answer is NO I want to empty the receipt from the queue. What is happening now is I will get 100 customers who say NO, the 101th says yes and the entire print queue prints 101 receipts (all the past "NO")
The Computer: Windows 10 and 11
Printer: STAR TSP700II receipt printer (printer designated on receipt report)
I have tried this code but get an error:
Dim qd As Object
Set qd = CreateObject("Shell.Application").NameSpace(16)
qd.ParseName("EPSON WF-2650 Series").InvokeVerb ("Empty")
ERROR: Object variable or With block variable not set
Actual printer name from printer properties: "EPSON WF-2650 Series" which is 21 characters
I call this code from a msgbox answer..........Call EmptyPrintQueue

This code cancels all print jobs from all printers:
Public Sub OhSht()
Dim o As Object, ret
For Each o In GetObject("winmgmts:{impersonationLevel=impersonate}//./root/cimv2").ExecQuery("Select * from Win32_Printer")
ret = o.CancelAllJobs
Debug.Print o.Name, ret
Next
End Sub

Related

Junction between categories

I am making a database for a freelance sign language interpreter. I have a subject table tblClient which holds information regarding the freelancer's clients. There is a lookup table tlkClientClientType showing the various types of clients (categorized on condition -- deaf, deaf/blind, etc). There is also a table called tlkClientModality. In this table are the different types of sign language offered by the interpreter. You can pick the client's preferred modality from the Client table.
Now the tricky part. There are certain modalities (basically just flavors of sign language) that should not be available to pick if the client is a certain type. For example, you cannot use "regular" (visual) sign language with someone who is deaf/blind. This is because regular sign language depends on the person being able to see. Instead, one would use "Tactile ASL" which is a hand-over-hand version of sign language. Basically, I want to limit the modality list based on the client type picked.
To start off my associations, I am making a junction table between the tlkClientClientType and tlkClientModality tables. Here, I will create the correct allowable pairs of client types and modalities. In addition, there is not really a necessary "junction field" to justify this relationship. I am considering making a "dummy field" as a way to still justify such a relationship.
Later, in a form for the client, I will edit the row source query on the modality combo box to be dependent on the choice selected in the client type box. This would be accomplished by checking what records in the junction table match the choice in the client type combo box.
Am I on the right track here? Making a junction table between two lookup tables seems weird. Is there anything wrong with it?
Note -- I would like to stay away from VBA, but I am up to writing macros to accomplish these goals. Any thoughts would be appreciated.
A photo of the relationships is given below.
Relationshipsphoto
Keep your tables in whatever normal form works for your business case. Your choice just changes how the information is stored which means you have to change how you write your queries as the information may be located in a different place. I chose a many to many relationship between client and clienttype.
You want to set the modality combobox contents from the client combobox. This is a problem of the view not the model hence we change the view and not the organization of the data. the key is to set the recordsource of the modality combobox from the afterupdate event of the client combobox.
Showing the results first: if the client is deaf we get:
if the client is blind or blind and deaf we get:
Private Sub cmbClient_AfterUpdate()
Dim RegularASL As Integer: RegularASL = 1
Dim TactileASL As Integer: TactileASL = 2
Dim ClientID As Integer: ClientID = Me.cmbClient
If ClientisBlindandDeaf(ClientID) Then
cmbModality.RowSource = "SELECT * FROM tlkClientModality WHERE ClientModalityID = " & TactileASL
ElseIf isClientBlind(ClientID) Then
cmbModality.RowSource = "SELECT * FROM tlkClientModality WHERE ClientModalityID = " & TactileASL
Else
cmbModality.RowSource = "SELECT * FROM tlkClientModality WHERE ClientModalityID = " & RegularASL
End If
cmbModality.Requery 'reload cmbmodality data
Me.Refresh 'repaint cmbmodality
End Sub
Public Function isClientBlind(ClientID As Integer) As Boolean
'Get rid of * in names vba can't parse the *
Dim isblind: isblind = 2
Dim ClientClientTypeID As Variant 'allows null return type
ClientClientTypeID = DLookup("ClientClientTypeID", "tlkClientClientType", "ClientID = " & ClientID & " AND ClientTypeID = " & isblind)
If IsNull(ClientClientTypeID) Then
isClientBlind = False
Else
isClientBlind = True
End If
End Function
Public Function isClientDeaf(ClientID As Integer) As Boolean
Dim isdeaf: isdeaf = 1
Dim ClientClientTypeID As Variant 'allows null return type
ClientClientTypeID = DLookup("ClientClientTypeID", "tlkClientClientType", "ClientID = " & ClientID & " AND ClientTypeID = " & isdeaf)
If IsNull(ClientClientTypeID) Then
isClientDeaf = False
Else
isClientDeaf = True
End If
End Function
Public Function ClientisBlindandDeaf(ClientID As Integer) As Boolean
If isClientBlind(ClientID) And isClientDeaf(ClientID) Then
ClientisBlindandDeaf = True
Else
ClientisBlindandDeaf = False
End If
End Function
note: cmbModality is set up just like it is bound to the entire tlkModality table then the record source changes are used to filter it in effect.

Filtering Information in A Report Using VBA-Access - Finding the next avaliable null field

I'm trying to organize information in a report in a certain way
I have 9 fields that will be used to store the label for the information
and another 9 fields that store the actual value of the information.
Whenever a field is full, I want it to place the information in the next available field. So that there will be no blank fields in between information.
The issue is that when trying to run this code which triggers two functions, it is not updating the report I referenced to in the two functions, nor is it pulling information from SpecSheetQuery, and I can't figure out why. My guess is that I'm not referencing the information correctly. I also have to figure out how to run this code individually on each record.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Servings = DLookup("[servings]", "SpecSheetQuery")
Calories = DLookup("[calories]", "SpecSheetQuery")
If Not IsEmpty(Servings) Then
OpenSlot ("Servings")
OpenFact (Servings)
End If
If Not IsEmpty(Calories) Then
OpenSlot ("Calories")
OpenFact (Calories)
End If
End Sub
OpenSlot function, puts title/label of information into field1 ( which is a field on the report) on the first null/empty field. so that no space is left blank.
Option Compare Database
Option Explicit
Public Function OpenSlot(Slot As String)
Debug.Print Reports!SpecSheet!field1.Value
If Reports!SpecSheet!field1.Value = Null Then
Debug.Print "Slot1"
Slot = Reports!SpecSheet!field1.Value
Debug.Print Reports!SpecSheet!field1.Value
ElseIf Reports!SpecSheet!field2.Value = Null Then
Reports!SpecSheet!field2.Value = Slot
End If
End Function
OpenFact function, puts fact into nut1 ( which is a field on the report) on the first null/empty field. so that no space is left blank.
Public Function OpenFact(fact As String)
If Reports!SpecSheet!nut1.Value = Null Then
Reports!SpecSheet!nut1.Value = fact
Debug.Print Reports!SpecSheet!nut1.Value
ElseIf Reports!SpecSheet!nut2.Value = Null Then
Reports!SpecSheet!nut2.Value = fact
End If
End Function
I'm trying to store this information into this report.
Any help would be greatly appreciated.

Pulling out specific text in an html file using vb.net

I am trying to get three values from a large html file. I thought I could use the substring method, but was informed that the position of the data may change. Basically, in the following code I need to pick out "Total number of records: 106", "Number of records imported:106", and "Number of records rejected: 0"
<B>Total number of records : </B>106</Font><br><Font face="arial" size="2"><B>Number of records imported : </B>106</Font><br><Font face="arial" size="2"><B>Number of records rejected : </B>0</Font>
I hope this is clear enough. Thanks in advance!
Simple string operations like IndexOf() and Substring() should be plenty to do the job. Regular Expressions would be another approach that'd take less code (and may allow more flexibility if the HTML tags can vary), but as Mark Twain would say, I didn't have time for a short solution, so I wrote a long one instead.
In general you'll get better results around here by showing you've at least made a reasonable attempt first and showing where you got stuck. But for this time...here you go. :-)
Private Shared Function GetMatchingCount(allInputText As String, textBefore As String, textAfter As String) As Integer?
'Find the first occurrence of the text before the desired number
Dim startPosition As Integer = allInputText.IndexOf(textBefore)
'If text before was not found, return Nothing
If startPosition < 0 Then Return Nothing
'Move the start position to the end of the text before, rather than the beginning.
startPosition += textBefore.Length
'Find the first occurrence of text after the desired number
Dim endPosition As Integer = allInputText.IndexOf(textAfter, startPosition)
'If text after was not found, return Nothing
If endPosition < 0 Then Return Nothing
'Get the string found at the start and end positions
Dim textFound As String = allInputText.Substring(startPosition, endPosition - startPosition)
'Try converting the string found to an integer
Try
Return CInt(textFound)
Catch ex As Exception
Return Nothing
End Try
End Function
Of course, it'll only work if the text before and after is always the same. If you use that with a driver console app like this (but without the Shared, since it'd be in a Module then)...
Sub Main()
Dim allText As String = "<B>Total number of records : </B>106</Font><br><Font face=""arial"" size=""2""><B>Number of records imported : </B>106</Font><br><Font face=""arial"" size=""2""><B>Number of records rejected : </B>0</Font>"""""
Dim totalRecords As Integer? = GetMatchingCount(allText, "<B>Total number of records : </B>", "<")
Dim recordsImported As Integer? = GetMatchingCount(allText, "<B>Number of records imported : </B>", "<")
Dim recordsRejected As Integer? = GetMatchingCount(allText, "<B>Number of records rejected : </B>", "<")
Console.WriteLine("Total: {0}", totalRecords)
Console.WriteLine("Imported: {0}", recordsImported)
Console.WriteLine("Rejected: {0}", recordsRejected)
Console.ReadKey()
End Sub
...you'll get output like so:
Total: 106
Imported: 106
Rejected: 0

Grab data from Mysql and loop through results

I need to check a mysql table periodically and if there are any rows found, I need to loop through them and perform some actions.
My SQL string is nice and simple: "SELECT * FROM 'dbcpman_jobs'".
There could be 1 returned row, or there could be 20 returned rows.
For each returned row, I need to assign some of the data to variables...
Dim job_id As String = jobrow.Item("id")
Dim job_jobid As String = jobrow.Item("jobid")
Dim job_status As String = jobrow.Item("status")
Dim job_dbxid As String = jobrow.Item("dbxid")
Then i need to make an API call using the information that's just been split out...
Try
Dim jobapicall As New System.Net.WebClient
jobcheckresult = jobapicall.DownloadString(fulljobapicheckurl)
Catch ex As Exception
Console.WriteLine("Error 'DBX-Err-1' - Error during API call")
End Try
Can someone point me in the right direction to loop through all rows found?
My code currently checks the first item found only, which is not ideal as if that job fails, everything else gets held up.
Thanks
FOR EACH loop was the answer.
something like this...
For Each jobRow As DataRow in dtJobResults.Rows
Try
what I want to do for each returned row.
Catch Ex as exception
messagebox.show("Oh no, something went wrong!")
End Try
Next

Access VBA Combo Box Reference

I am running into a snag on the code below. When the code runs, values are being placed on "tblPrepayments", with the exception of the AccountID value. The me.cboAccountID.column(2) is on "frmInvoices", which pulls in the AccountID, but displays a clients name for usability reasons.
I don't get any errors, but the value is not pulling into "tblPrepayments". What am I missing? Please let me know if you need additional clarification.
If [Rec'd_Prepayments] <> "0.00" And [Prepayment_Month] <> "" Or [Prepayment_Year] <> "" Then
Dim RecSet As Recordset
Set RecSet = CurrentDb.OpenRecordset("tblPrePayments")
RecSet.AddNew
RecSet![AccountID] = me.cboAccountID.column(2)
RecSet![Prepayment_Month] = "Billing_Month"
RecSet![Prepayment_Year] = "Billing_Year"
RecSet![Rec'd_Prepayment] = "Prepayment1"
RecSet.Update
End If
End Sub
If I run MsgBox me.cboAccountID.column(2), I get a run time error '94': Invalid use of Null. If I change the code to Msgbox me.cboAccountID.column(1), I get the client's name, not the ID, and subsequently an error for mismatched data types.
Here is the row source for cboAccountID.
SELECT tblClientLists.[AccountID], [tblClientLists].Invoice_To
FROM tblClientLists
ORDER BY [Invoice_To];
combobox values are 0 based, so you're looking for Me.cboAccountID.column(0) or Me.cboAccountID.Value should also work here.