in select statement how we compare a string field which contain single quotes - vb6

i have a filed in stock table with title product. when i use the following statement
rs.Open "select * from stock where product='" & product_name & "'
",db,2,1
id = rs!sub_head_id
rs.Close
where product_name is string variable which contain a product name
for example:
product_name="alpha's cell"
this statement make an error, because of single quotes in string.
how we resolve this
note: i am using vb6

Use this and try:
rs.Open "select * from stock where product='" & replace(product_name,"'","''") & "' ",db,2,1

Ok , my problem is solved . some one suggest that set the Index of Criteria field like bno and productid .

Related

ALL parameter for SSRS Report

I want to be able to let the business manually enter a USERID into a parameter box or leave it blank for it to bring back all USERID's.
I have setup another parameter for CUSTOMERID which uses a dataset to bring back a list of customer ID's. Due to the amount of USERID's I don't want to use the same solution but instead have them manually enter the USERID or leave it blank and bring back ALL.
SELECT
CUSTOMERID,
CASE
WHEN STATUSCODE = 200 THEN 'Successful Logon'
ELSE 'Unsuccessful Logon'
END as LogonStatus,
COUNT( * ) COUNTOFACCOUNTS
FROM
MA4EQNG.APPLICATIONLOG
WHERE
CUSTOMERID in ('"+join(Parameters!CustomerID.Value, "','")+"')
AND (Cast(DATETIME as Date) >= '"& Format(Parameters!FromDate.Value, "yyyy-MM-dd") & "'
AND Cast(DATETIME as Date) <= '" & Format(Parameters!ToDate.Value, "yyyy-MM-dd") & "')
AND COMPONENTDESCRIPTION = '/eq/auth/v1/logon'
AND METHOD = 'POST'
GROUP BY
CUSTOMERID,
CASE
WHEN STATUSCODE = 200 THEN 'Successful Logon'
ELSE 'Unsuccessful Logon'
END
ORDER BY
CUSTOMERID ASC
Please let me know if you need anything else.
You can set the USERID parameter to allow blank value then use IF condition on your main dataset sql script.
Try
IF LEN(#USERID)>0
*put the sql script with UserId filter in where clause*
ELSE
*put the sql script WITHOUT UserId filter in where clause*
Normally you would just script something like this in the WHERE clause.
WHERE
isnull(#CustomerID,'') = '' OR (CUSTOMERID in('"+join(Parameters!CustomerID.Value, "','")+"'))
And long term, I would create a function that takes in a delimited string, and converts them to rows, then you can just join on the results from the table valued function.
How to split a comma-separated value to columns

Syntax Error in query expression with vb 6.0

sql = "update Attendance set Attended=Attended+1 where Student ID like 15001"
db.Execute (sql)
is is showing :
"syntax error(missing operator) in query expression 'Student ID like
15001'
If realy you have a space between Student and ID tehn your query must be :
sql = "update Attendance set Attended=Attended+1 where [Student ID] like 15001"
db.Execute (sql)
sql should always be written with an _. For Example Student ID should always be Student_ID so that it has the idea of reading which column it needs to filter its search. For future purposes always create a column name with an _.
sql = "update Attendance set Attended=Attended+1 where Student_ID like 15001"
db.Execute (sql)
or
sql = "update Attendance set Attended=Attended+1 where [Student ID] like 15001"
db.Execute (sql)
Try something that looks a bit cleaner...
sql = "Update tblAttendance SET "
sql = sql & " Attended = Attended + 1 "
sql = sql & " Where Student_ID = 15001 "
db.execute SQL <-- no paranthesis

Only 1 row in recordset but all rows in table get updated

The query retrieves a single record as is confirmed by the recordcount but every single row in the table gets updated
I am using vb6 and ms ado 2.8
The Firebird version is 2.5.4.26856 (x64).
Firebird ODBC driver 2.0.3.154
The computer is windows 7 home edition 64 bit
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cs As String
Dim dbPath As String
dbPath = "c:\Parkes\Parkes.fdb"
cs = "DRIVER={Firebird/Interbase(r) Driver}; DBNAME=localhost:" & dbPath & "; UID=SYSDBA; PWD=masterkey;"
cn.ConnectionString = cs
cn.Open
Dim sQuery As String
sQuery = "select memo from clients where clientID = 10021 "
rs.Open sQuery, cn, adOpenStatic, adLockOptimistic
If rs.BOF <> True Or rs.EOF <> True Then
'putting msgbox rs.recordcount here confirms only 1 record in recordset
rs.Movefirst
rs.Fields("memo") = "blah"
rs.Update
End If
Set rs = Nothing
Set cn = Nothing
If I alter the query slightly by also selecting a second column, the client surname then only rows with the same value in the surname column as that of of the row where the clientid is 10021 get edited.
sQuery = "select memo, surname from clients where clientID = 10021 "
I cannot understand how more than one row should be edited when the recordset contains only a single row
EDIT: Having read around the web a bit this is my understanding of what is happening.
It seems that the update method identifies which records to update based on the selected columns in the recordset.
So if you select fields a,b,c,d and are updating field a, it will only update records in the database whose values for a,b,c,d match those in the recordset.
The best way to ensure that you only update a single record is to include the primary key in the selected fields.
So if I had written my query as in the line below, only a single record would have been updated because the clientID column contains unique values.
sQuery = "select memo, clientID from clients where clientID = 10021 "
It makes sense thinking about it but the way I wrote the query originally seems to work fine, in my experience, with other databases or am I wrong?
I tested your code everything was fine and only update one row. I only want to suggest you a simple way to check whether record exist or not. that could be like this:
if rs.rows.count > 0 then
' no need to move recordset to first by default its on the first row
end if

Read one column values from database and and sort randomly and put the sorted values into other column

I have a requirement to read one complete column values from Access database and need to sort it in a random order/ Descending order and update the sorted values to another column so , that one name is assigned to another name. That to only using VB script.
I know only how to read the values from Access database i don't know further steps..
Could any one help me in this?
One Recordset sort Desc, second Recordset sort Asc. Then loop through the first RS and update the table with the first RS value where table value equals second RS value.
In this example change ColumnName, ColumnName2 and TableName according to your database. ColumnName2 is output column so it will be replaced with new values.
Sub AddDescToAsc()
Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MyRS2 As DAO.Recordset
Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset("SELECT ColumnName FROM TableName ORDER BY ColumnName DESC", dbOpenForwardOnly)
Set MyRS2 = MyDB.OpenRecordset("SELECT ColumnName FROM TableName ORDER BY ColumnName ASC")
MyRS2.MoveFirst
With MyRS
Do While Not .EOF
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE TableName SET ColumnName2 = '" & .Fields("ColumnName") & "' WHERE ColumnName = '" & MyRS2(0) & "'"
DoCmd.SetWarnings True
MyRS2.MoveNext
.MoveNext
Loop
End With
MyRS.Close
MyRS2.Close
End Sub

When I select the value from the combo box, related value should appear in the text box

When I select the value from the combo box, related value should appear in the text box
ComboBox code.
cmd.CommandText = "select distinct PERSONID from T_PERSON"
Set rs = cmd.Execute
While Not rs.EOF
If Not IsNull(rs("PersonID")) Then
txtno.AddItem rs("PersonID")
End If
rs.MoveNext
Wend
In comboBox list of ID is displaying, when I select the particular person id, Name should display in text box related to the personid
Text Box
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
Set rs = cmd.Execute
While Not rs.EOF
If Not IsNull(rs("Name")) Then
txtName.Text = rs("Name")
rs.MoveNext
End If
Wend
I put the above code in Form_Load Event, Nothing displaying in Text Box.
What wrong in my code.
Need VB6 code Help
You would want the 2nd block of code in the the click event for the combobox.
Edit
There looks like another couple of issues in your code at this line:
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno & " '"
2 Issues:
You are passing in the control itself as the person ID, not the selected value.
You have an extra space in your query after the person ID
You should change that line to be:
cmd.CommandText = "select distinct Name from T_Person where personid = '" & txtno.SelectedItem.Text & "'"
Why not have the combobox display the name and hold the personID as it's item data?
cmd.CommandText = "select distinct PERSONID, Name from T_PERSON WHERE PersonID IS NOT NULL"
Set rs = cmd.Execute
While Not rs.EOF
combo.AddItem rs("Name").value
combo.ItemData(combo.NewIndex) = rs("PERSONID").value
rs.MoveNext
Wend
Then, if you need the PersonID for the selected name you can just grab combo.ItemData(combo.ListIndex).

Resources