User-entered number of pages to print in MS Access VBA - sql

Is it possible to have a user enter the desired number of pages they wish to print onto a form, and be able to print "Page X of Y" on each page?
I have a small shipping label application that requires a dynamic number of labels to be printed for any given shipment, and is entered by the user.
The data used for the label information is returned from the following stored procedure
SELECT C1.CompanyName,
C1.Address1,
C1.Address2,
C1.City,
C1.Province,
C1.PostalCode,
C2.CustomerName,
C2.ShippingAddress1,
C2.ShippingAddress2,
C2.ShippingAddress3,
C2.ShippingAddress4,
C2.City,
C2.Province,
C2.PostalCode
FROM MyCompany_tb C1,
Customer_tb C2
WHERE C2.CustomerID = #CUSTOMER_ID
AND C2.InvoiceNumber = #INVOICE_NUMBER
This will produce a single record that includes my company "return address" information (C1), and pairs it with the the customer's shipping information (C2). The label(s) that get
printed need to print out "Box 1 of 3", "Box 2 of 3", etc, depending on the value entered by the user.
I found this article, but I would prefer to do this without creating a "temp table" in Access. Is something like this possible?

You need an additional table, but it can be a simple number table, with one LongInt column ID with numbers 1..n
Then use a cartesian product to get the number of records you want.
In VBA:
' lngNumRecords is the number entered by the user
TempVars.Add "NumRecords", lngNumRecords
The query:
SELECT YourQuery.*, "Box " & [tblNumbers].[ID] & " of " & [TempVars]![NumRecords] AS BoxNo
FROM YourQuery, tblNumbers
WHERE tblNumbers.ID <= [TempVars]![NumRecords]
There is no JOIN, so you get your main record replicated as many times as IDs are specified.
This assumes that YourQuery returns exactly one record.

Related

Referencing SQL fields from a DataSet where 2 field names are the same

I have the following SQL query which I am loading in to a DataSet:
SELECT i1.* , i2.* From tblMMLettersImportTable i1 Join tblMMLettersImportTable i2 on i1.SectionID + 1 = i2.SectionID Where i2.startpage - i1.endpage <> 1
Idea is to check that the index for various sections of a document lead one page on to the other with no gaps. I.e section 2 ends on page 5 and section 3 starts on page 6.
I'm happy that the SQL works, however by joining on itself the field "SectionID" is duplicated. In SQL easy enough, just use i1. or i2. to reference the correct one.
The issue comes when I load this in to a VB.net Dataset. I need to raise an error message with something like:
MessageBox.Show("There is a page gap between sections " & row.item("i1.sectionID") & " and " & row.item("i2.sectionID")
I get the error message Column 'i1.intline' does not belong to table Table. Makes sense as that is not its name in the dataset. I've considered using the column number to reference the item to pull out, however the SQL Table tblMMLettersImportTable is deleted, created and populated dynamically depending on the type of Letter/document being produced so I cannot always guarantee that the columns will always numbered the same. This is also why i1.* and i2.* is used instead of listing each column.
Is there a way that I can reference 2 items in a DataSet that have the same item name with VB.Net?

Add Items of Multiple Orders into a Single Bill (Crystal Report)

I'm working in visual studio with vb.net and crystal report where I have few tables that store the order information of any restaurant. I want to print the bill of any order for the guest and also in case if guest has placed the order multiple times in a restaurant.
I'm attaching a image that will give you some sort of details about the scenario where 3 tables are designed with bill format as a output.
I want to use a record selection formula for any particular table to print the items details in Bill for all the order placed. Table 2 & 3 are linked to each other with 'Order No'. When I give the command to print the bill for table 'T1' it should give me the details as shown in the bill format which include all the order items from table 'TableOrderedItems' for the list of order numbers placed for that particular table (i.e. T1).
Firstly, I create a list of all orders that pertain to table # 'T1' like ("K1", "K2"). Then I will look into table 'TableOrderedItems' for each order no. in list and get printed all the items in bill (see image).`
Dim cryRptBill As New ReportDocument
Dim RepLocationBill As String
RepLocationBill = Application.StartupPath & "\CryRptBillTable.rpt"
cryRptBill.Load(RepLocationBill)
cryRptBill.RecordSelectionFormula = "{TableOrderedItems.OrderNumber}=" & " in [" + kotList + "]"
Image For Tables & Bill Format
I think you want this:
CryRpt.RecordSelectionFotmula= "{tblUsers.ID} in [" + List + "]"
This assumes your list is a string with number and commas. For example "1, 3, 4"

Retrieving Columns with count greater than 1 - Google Sheet Query

I'm using Google sheets, and I want to get the data from one sheet to another where I want only the columns with count > 1.
Let's say we have 3 columns A, B, and C. I tried the following (the first sheet name is "Form Responses 1"):
I thought about using a query in the second sheet as: =query('Form Responses 1'!A1:Z, "Select A having count (A) >1 union select B having count (B) >1 union select C having count (C) > 1"). But I got a parse error where it seems that union and having are not supported in google sheets query.
How can I achieve this (whether it's using query or any other Google sheets function that can work)?
More details:
The first sheet contains info about exercises conducted during a lecture and it gets its data from a Google Form (so the responses are fed in this sheet). Here is a screenshot of it:
Please note that the form is divided into sections. When the user selects the course, the attendance, the participation, and adds a comment, then they go to the next section, the next section will be based on the selected course, the newly opened section will have the exercise name and rating questions (the exercise name is a dropdown list with items that are prefilled and specific to the selected course). That's why, you can see that "exercise name" and "rate the exercise" columns are repeated because we have 2 sections in this form.
The second sheet should contain the data of a selected course only (either mobile dev or web dev) which can be achieved easily through a query with a where clause. But, in addition to that, it shouldn't contain the empty columns of "exercise name" and "rate the exercise" as they correspond to another section. So, it should have only one exercise name column and one rating column that correspond to the selected course. Here is a screenshot if we only use a query with where clause without removing the extra name and rating columns:
Here is a screenshot with the desired result:
Thanks.
why not use just:
=QUERY('Form Responses 1'!A1:Z, "select A,B,C,D,E,F,G where F is not null", 1)
Use "OR" condition
Eg:-
QUERY(Data!A:R,"select A, N, P where N>0 or P>0")
where A column has country and N, P columns have population values

Read (lookup multiple criteria) records from database table, and Write to database table with matching criteria

How do I replicate the equation below in Access Query or in VB.net code?
{=SUM(IF('Customer '!$G$3='Glass Fabrication'!$D$3:$G$3,IF("Hole"='Glass Fabrication'!$A$4:$A$15,'Glass Fabrication'!$D$4:$G$15)))*E3}
The above equation is what I use in excel to do Vlookup more than 1 value and returns a value that matches 2 criteria. (Now, this can go on forever, and have as many criteria as possible)
Now, I am working on VB.net to make a Quotation system with the MS Access database (.mdb).
I have a table that lists all products that have several identifiers with 4 different prices. For the simplicity, let's say I have 2 identifiers and 4 prices.
I have fields like Name Category as identifiers, and sample data looks like this.
Name Category Tier 1 Tier 2 Tier 3 Tier 4
Apple...Fruit............$2..........$2...........$1.7........$1.5
Apple...Juice............$1..........$1...........$.75........$.75
Orange..Fruit.........$1.8.......$1.8.........$1.5.........$1.3
Coke...Drink............$1..........$1...........$.75........$.75
User Input = Apple , Juice , Tier 1
I can make =Sum(If formula in excel to check for Name column to match selected value, then check the Category column to check the 2nd value, then check the 1st Row header for Tier 1 value and returns $1.
I am very new to Access Database, so I have a hard time how to replicate this. I have tried making a query, but it only "filters" such criteria, and doesn't return a value.
Ultimately, I would want a table/query called Shopping Cart that accepts user input such as "Apple, Juice, Tier 1" and 4th Column(read-only) would automatically calculate and find that Price of $1 by looking up those 3 things.
I have set up the shopping cart table already, and it works perfectly except I don't know how to include this read-only automatically calculated/vlookup column. (I am using DataGridView in VB.net to bind to a shopping cart table that takes 3 user inputs.)
What would be the best way to do this? Make Query in Access and make it calculate in the database itself or VB.net to handle the calculation and write into the 4th column everytime user inputs 3 values?
Try this SQL query in MS Access, provided your example data is saved as an Access table:
SELECT Table.Name, Table.Category, [Enter Tier],
IIF([Enter Tier] = 'Tier 1', Table.[Tier 1], IIF([Enter Tier] = 'Tier 2', Table.[Tier 2],
IIF([Enter Tier] = 'Tier 3', Table.[Tier 3], IIF([Enter Tier] = 'Tier 4', Table.[Tier 4], Null)))) As Price
FROM Table
WHERE Table.Name = [Enter Name] AND Table.Category = [Enter Category]
The Enter fields will pop up for user to fill as parameter requests when you open query. It would be better to use an Access form for user to fill unbound textboxes and command button opens query using textbox values. Form fields can replace above Enter fields.
Dim row AS DataRow = DataTable.Select("ColumnName1 = 'value3'").FirstOrDefault()
If Not row Is Nothing Then
searchedValue = row.Item("ColumnName2")
End If
Here value3 is a constant value. To use objects(variables),
Dim row AS DataRow = DataTable.Select("ColumnName1 = '" & variable & "'").FirstOrDefault() was used.

Use tekst from table depending on number in other table using Access

I feel stupid asking this, but I really need an excample on how to get a value of a field in one table (in the end in my report) depending on a value of a field from an other table in Access.
So I have (for excample) a table:
Products and in my report I do a formule using the value of price (field of Products) and adding to that I must have the value of the field VAT-Type (a nummeric var, in the table VATS) depending on what is there in the record (of the one in the table Products) in the field VAT-Sort, also a nummeric var that must meet one of the values used in the field VAT-Type).
So in the report I must have something like:
Product: X Count Price'=(price+21%)'
where 21% comes from the dependensy between the field VAT-Type and VAT-Sort.
I know I can do something like result=select 'VAT-Sort' from 'VATS' WHERE 'VAT-Sort' = or equals 'VAT-type'
But how do I use it in a report of Access to get the right result?
You can use DLookUp:
Numeric data type:
DlookUp("Value","Vats","Vat_Type=" & Vat_Sort)
Text data type:
DlookUp("Value","Vats","Vat_Type='" & Vat_Sort & "'")
Or you can base your report on a query, say:
SELECT Value, Other, Field, names FROM Products
LEFT JOIN Vats
ON Products.VAT_Sort = Vats.Vat_Type
Edit re comments