How to get sets of Records in VB6? - vb6

Hey guys, just want to ask you a simple question that I know you're familiar with... I am using VB6, I just want to get sets of records from my database. What I mean is that I have UserID and with a part of code provided below, it only gets a single set of record. Like for instance, the value of UserID is A12, and so, all sets of records with the UserID of A12 must display in Textboxes respectively with the aid of datPayroll.Recordset.MoveNext.
With datPayroll
.RecordSource = "select * from tblpayroll where empid like '" & UserID & "'"
.Refresh
Me.txtRegularHours.Text = .Recordset.Fields!reghours
End With
-datPayroll : DataControl
-txtRegularHours : Textbox
-UserID : Variable

You probably want to look at MoveFirst, MoveNext, etc. and also EOF
Here is a link or two to get you started:
EOF, BOF
MoveFirst, MoveNext
You need to check that you have some data in your Recordset using EOF, then MoveFirst to move to the first record, and loop through using MoveNext.

Related

MS ACCESS table default value code line for autogenerated sequential and unique alphanumeric number

I am new to MS Access and I would like to generate an autogenerated sequential and unique alphanumeric number of the format SYYMM001, SYYMM002, SYYMM003... (ex for 2023 january: S2301001, S2301002, S2301003).
I use MS Access 2016.
I am in my table, in View mode, in the column InvoiceCode in which I want the number to appear, in the general sheet, in Default Value I used the following code:
= "S" & Format(Now(),"yymm") & Format((DCount("[InvoiceID]","InvoiceTable")),"000")
where InvoiceID is the autonumber column and InvoiceTable the name of the table.
This code does not work and generate the following error:
"Unknown function "Dcount" in validation expression or default value on "Invoice Table.InvoiceCode"
I tried another code that I found online which works but instead of giving me a sequential number it generate a random number ex S2301586, S2301236 ...
="S" & Format(Now(),"yymm") & Format(Int(Rnd()*1000),"000")
Would you have a code that would do what I need?
Thanks in advance for your help
You can't set this in the table.
You could try this in your form you use for data entry - in the BeforeInsert event of the form:
Me!InvoiceID.Value = "S" & Format(Date,"yymm") & Format(DCount("*","InvoiceTable"),"000")

Cannot display correct records in unbound form in Access 2013

I have one Table called tblEmployees - ID, First, Last. One unbound form called frmSearch with a textbox named Searchbox and a search button that searches by ID.I have one more unbound form called frmDisplay that displays the search result in it which I would like to edit when necessary. The textbox fields for this form are EID, Fname, Lname.
The problem I am having is when I enter the ID# in the searchbox and click the search button (where I linked both ID fields in the button wizard) it keeps on displaying the second record in my table. This is the code I currently have running
Private Sub Form_Load()
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("SELECT * from tblEmployees WHERE ID=ID")
EID.Value = rst!ID
Fname.Value = rst!First
Lname.Value = rst!Last
end sub
When I change the code to read
("SELECT * tblEmployees WHERE ID=" & ID")
I get a syntax error missing operator in query expression ID="
ID must have some value. Then concatenate ID:
"Select * From tblEmployees Where ID = " & ID & ""
The recordset that's currently being opened will include all the records in your table, because for every record the condition ID = ID will always be true. It's purely coincidence that it's displaying the second record from your table. You didn't specify a sort on the recordset query so it could randomly pick up any record as the first one in the results. What you actually need is
Set rst = CurrentDb.OpenRecordset("SELECT * from tblEmployees WHERE ID = " & frmSearch!Searchbox)

How to display drop down options alphabetically without worrying about case sensitive in classic asp?

I have a drop down box in my classic ASP page and using MS SQL for database. The drop down list includes the brand names starting with lower case, upper case and starting brand name with numbers.
For instance, itcosmetics, Colorpop Cosmetics and 5 Hour energy respectively. Currently, this dropdown is showing/displaying brands alphabetically but by case i.e. lower case, number, upper case.
In other words, it is displaying all lower case brand name alphabetically first then the brand with numbers alphabetically and finally brand names starting with uppercase alphabetically.
However, what I am trying is : I want to display my options according to the alphabetical orders without worrying about the case of the brand name.
for example: if the brand names are 1 apple, applea, Appleb, 3 fans, balla, Ballb, cat, Doll.
Currently, its displaying drop down option as following:
applea
balla
cat
1 apple
3 fans
Appleb
Ballb
Doll
But I want something like following:
1 apple
3 fans
applea
Appleb
balla
Ballb
cat
Doll
How can I do that?
My code :
<select>
<OPTION value=0>-- SELECT --</OPTION>
<%
DIM RS, varQueryBrand, varBrand
IF Request.QueryString("brandID") <> "" THEN
varQueryBrand = Request.QueryString("brandID")
ELSE
varQueryBrand = "SELECT"
End IF
SQL = "SELECT DISTINCT(brand) as brand FROM tblproduct"
SET RS=objConn.Execute(SQL)
IF NOT (RS.BOF and RS.EOF) THEN
WHILE NOT RS.EOF
varBrand = RS("brand")
IF LCase(varQueryBrand) = LCase(varBrand) THEN
Response.Write "<option selected value=""" & replace(RS("brand")," & ","#") & """>" & RS("brand") & "</option>"
ELSE
Response.Write "<option value=""" & replace(RS("brand")," & ","#") & """>" & RS("brand") & "</option>"
End IF
RS.MoveNext
WEND
END IF
RS.close
SET RS = nothing
%>
</select>
I tried writing ORDER BY brand at the end of SQL statement, but there was no chage in the output. So can you please help me?
You are likely using a Collation with the CS designation in it like Latin1_General_CS_AS for example.
To avoid the Unicode Sorting, switch to a Binary Collation to get the expected sort order.
SELECT DISTINCT brand
FROM tblproduct
ORDER BY brand COLLATE Latin1_General_bin
A slightly messier approach is to use a sub query to wrap a lowercase version of the Brand column and use that for sorting.
SELECT x.brand
FROM (
SELECT DISTINCT brand, LOWER(brand) [brand_lower]
FROM tblproduct
) x
ORDER BY x.brand_lower
As #Martha point's out don't forget to specify an ORDER BY in your SQL string in Classic ASP.
Useful Links
ORDER BY … COLLATE in SQL Server
From what it looks like, you aren't currently sorting your results at all, so you're getting whatever default order SQL Server cares to come up with.
Most databases are automatically set to case-insensitive sorting, so unless your database is set up strangely, the following should work:
SQL = "SELECT DISTINCT brand FROM tblproduct ORDER BY brand"
If that doesn't work and you don't feel like messing around with the COLLATION settings, you can sort by an all-lowercase (or all-uppercase) version of the field:
SQL = "SELECT DISTINCT brand FROM tblproduct ORDER BY LOWER(brand)"

Calculated control displays the total number of records that appear in the subform

My assignment is to create a calculated control that displays to total number of Members in the subform. How can I accomplish this when there is no definitive field that I can use in the expression. There are only three fields in the subform: First Name, Last Name, and Phone. If I do something like this =[frmPlanMemberSubform].[Form]![FirstName] that only calculate and displays the first name of the member in the subform. Actually there are only two names in the subform. Theoretically I suppose to get back a count of 2. But I can't figure out how to do it with the existing fields in the subform. Any Access experts out there? Please help. Here is what the database looks like in form view.
As you can see there is nothing in the Total Members control box.
Follow these steps:
1) In the code of the master form insert a function similar to this:
Private Function NumRecords()
Dim rec As Recordset
On Error GoTo lbErr
Set rec = Me!<subform-name>.Form.RecordsetClone
rec.MoveLast
NumRecords = rec.RecordCount
lbExit:
Exit Function
lbErr:
MsgBox Error, vbExclamation
Resume lbExit
End Function
2) In the field to display the number of records insert the following string in the value property:
=NumRecords()
3) Create the Form_Current trigger as follows:
Private Sub Form_Current()
Me!<fieldname>.Requery
End Sub
enter image description here

ASP Classic SQL Query error message, right syntax please

I am passing two (2) parameters in the URL, and building the following SQL:
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num =" & request.querystring("num") & "AND name LIKE" & request.querystring("nam")
I got an error message:
Microsoft OLE DB Provider for Oracle error '80040e14'
ORA-00933: SQL command not properly ended
What would be the right syntax for this?
You need to put quotes around the LIKE clause. Also, you could consider using percents for wildcard matching
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num =" & request.querystring("num") & " AND name LIKE '%" & request.querystring("nam") & "%' "
Part of your problem may be the improper spacing around the quotes where you're inserting the values. This:
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num =" & request.querystring("num") & "AND name LIKE" & request.querystring("nam")
Will most likely result in sending this to the database:
SELECT DISTINCT name
FROM link3
WHERE invoice_num =2AND name LIKEsomeothervalue
If you add proper spacing like this:
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num = " & request.querystring("num") & " AND name LIKE " & request.querystring("nam")
It would give you a more properly formatted result like this:
SELECT DISTINCT name
FROM link3
WHERE invoice_num = 2 AND name LIKE someothervalue
Any time I get errors that indicate problems with SQL formatting/structure I tend to log the SQL that is sent before it goes to the database. This helps spot odd issues like that.
Also, sharpguru is probably right - the LIKE clause probably isn't formatted correctly either. You need to enclose text values in single-quotes, and the % is a wildcard matching 0 or more characters - making this more like what you are probably looking for:
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num = " & request.querystring("num") & " AND name LIKE '%" & request.querystring("nam") & "%'"
Now, this does all assume that invoice_num is some sort of numeric value - which is implied in your question and code. However, if it is not (as suggested by your comment and other questions), you would need to put the value in single quotes - just like any other text field in almost all RDBMSs:
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num = '" & request.querystring("num") & "' AND name LIKE '%" & request.querystring("nam") & "%'"
The above would also be used if the data type of the database column invoice_num was set to a non-numeric data type. Just because the data is somehting that could be called numeric, doesn't mean it's automatically treated as numeric. If the data type of the column is text, ntext, or any other non-numeric type, then you will need to surround the value in quotes just like any other text value.
And, while not related to the question, I'm hoping this is an over-simplified example and you're not directly inserting QueryString values into the SQL statement. If you haven't been told yet, that's opening you up to a wide variety of security problems - look up some information on SQL Injection.

Resources