From This question, its answer is almost my answer. But I am facing some sql query issue, I have the following statement in VB
Dim results As DataRow() = table.Select("A = 'foo' AND B = 'bar' AND C = 'baz'")
I want to place foo, bar and baz in variables and use that variables in above statements.
Dim Varfoo As String = "foo"
Dim Varbar As String = "bar"
Dim Varbaz As String = "baz"
I managed to get one variable in statement as
Dim results As DataRow() = table.Select("A = " + Varfoo)
But how to insert multiple sort expressions with variables?
Edit: I got it solved with the answer of vikas as following;
Dim results As DataRow() = table.Select("A = '" & Varfoo & "' And B = '" & Varbar & "' And C = '" & Varbaz & "'")
Have you tried
Dim results As DataRow() = table.Select("A = '" & Varfoo & "'")
Edited
For OR operation
Dim results As DataRow() = table.Select("A = '" & Varfoo & "' OR B = '" & Varbar & "' OR C = '" & Varbaz & "'")
For AND operation
Dim results As DataRow() = table.Select("A = '" & Varfoo & "' AND B = '" & Varbar & "' AND C = '" & Varbaz & "'")
Related
This is the last problem I have to face in for my capstone project, and it's driving me nuts.
Basically, I have to be able to identify if Section/Faculty/Room are all in use when scheduling a subject, to avoid conflicts.
Here's what I've worked on, but so far it can only detect when Room is in use.
I can't figure out how to be able to prevent scheduling that's in-between time periods. For example: First entry would be 7-8:30AM. Second entry would be 7:30 AM to 9 AM. With the former existing, it should reject the latter but I can't figure out how to do that. This is what I've cooked up so far. How would you guys go about this?
Public Function DataInUse() As Boolean
Dim Temp As Boolean
Temp = False
If FacultyInUse() = True Then
MessageBox.Show("Faculty in use.")
cboFaculty.Focus()
DisplayFacultyInUse()
DisplayLabelConflictForFaculty()
Temp = True
ElseIf RoomInUse() = True Then
MessageBox.Show("Room in use.")
cboRoom.Focus()
DisplayRoomInUse()
DisplayLabelConflictForRoom()
Temp = True
End If
Return Temp
End Function
Public Function FacultyInUse() As Boolean
Dim com As New OleDbCommand(" Select * from qrySubjectOfferring Where cTimeIn >=#" & cboFrom.Text & "# and cTimeOut <=#" & cboTo.Text & "# and Faculty like'" & cboFaculty.Text & "%' and cDay Like '%" & cboDay.Text & "%'", clsCon.con)
Dim dr As OleDbDataReader = com.ExecuteReader()
dr.Read()
If dr.HasRows Then
Return True
Else
Return False
End If
End Function
Public Function RoomInUse() As Boolean
Dim com As New OleDbCommand("Select * from qryRoomAvailability WHERE (cTimeIn <=#" & cboFrom.Text & "# AND cTimeOut >=#" & cboFrom.Text & "# AND Room = '" & cboRoom.Text & "' AND cDay = '" & cboDay.Text & "') OR (cTimeIn <=#" & cboTo.Text & "# AND cTimeOut >=#" & cboTo.Text & "# AND Room = '" & cboRoom.Text & "' AND cDay = '" & cboDay.Text & "') OR (cTimeIn >= #" & cboFrom.Text & "# AND cTimeOut <= #" & cboTo.Text & "# AND Room = '" & cboRoom.Text & "' AND cDay = '" & cboDay.Text & "') ", clsCon.con)
Dim dr As OleDbDataReader = com.ExecuteReader()
dr.Read()
If dr.HasRows Then
Return True
Else
Return False
End If
End Function
Public Function SubjectAlreadyOffered(sSubject As String) As Boolean
Dim com As New OleDbCommand("Select * from qrySubjectOfferring Where Subject LIKE '%" & sSubject & "%'", clsCon.con)
Dim dr As OleDbDataReader = com.ExecuteReader()
dr.Read()
If dr.HasRows Then
Return True
Else
Return False
End If
End Function
Try simplifying your SQL to use Between clause and eliminate all entries where the From or To are in existing entries
In your RoomInUse() function
Dim strSQL as String
strSQL = "SELECT * FROM qryRoomAvailability WHERE " & _
" Room = '" & cboRoom.Text & "' AND cDay = '" & cboDay.Text & "'" & _
" AND NOT (#" & cboFrom.Text "# BETWEEN [" & cTimeIn & "] AND [" & cTimeOut & "])" & _
" AND NOT (#" & cboTo.Text "# BETWEEN [" & cTimeIn & "] AND [" & cTimeOut & "])"
Dim com As New OleDbCommand(strSQL, clsCon.con)
I have two recordset which want to update one of them by value of the other. I did like this
stSql = "SELECT dbo.tblCableProperty.CatalogCode FROM dbo.tblCable INNER JOIN " & _
" dbo.tblCableProperty ON dbo.tblCable.CablePcode = dbo.tblCableProperty.CablePcode" & _
" WHERE dbo.tblCable.prjsubcode=" & prjsubcode & " AND dbo.tblCable.Active=1 " & " And dbo.tblCable.Gtag='" & Gtag & "' And dbo.tblCable.TagNo=" & tagno & " And dbo.tblCable.NTag=" & NTag & " And dbo.tblCable.EndStr='" & EndStr & "'"
rs.Open stSql, cn, adOpenStatic, adLockOptimistic
catalogCode = rs!catalogCode
rs.Close
stSql = "SELECT *,'' as ShowNum FROM viwShowNum WHERE prjsubcode=" & prjsubcode & " AND Active=1 " & " And Gtag='" & Gtag & "' And TagNo=" & tagno & " And NTag=" & NTag & " And EndStr='" & EndStr & "' ORDER BY 8"
rs.Open stSql, cn, adOpenDynamic, adLockOptimistic
rs.MoveFirst
stSql = "Select * from tblCoreCode where CatalogCode=" & catalogCode
Set rsCoreCode = New ADODB.Recordset
rsCoreCode.CursorLocation = adUseClient
rsCoreCode.Open stSql, cn, adOpenStatic, adLockOptimistic
While Not rs.EOF
criteria = "RealNum='" & rs!CoreNo & "'"
rsCoreCode.Filter = criteria
rs!ShowNum = CStr(rsCoreCode!ShowNum)
rsCoreCode.Filter = adFilterNone
rs.MoveNext
Wend
I get the following error on this part
rs!ShowNum = CStr(rsCoreCode!ShowNum)
multiple-step operation generated errors. check each status value
rsCoreCode!ShowNum is varchar(5). I tried to set the value
rs!ShowNum = "1"
but again I got the same error.
where is the problem?
Thank you
As asked in my comment, if rs.Updatable or rs!ShowNum.DataUpdatable are false, you can use this piece of code from Microsoft to retrieve an updatable RecordSet.
Same issue occurred to me the problem was that i violated an object property , in my case it was size the error came out as
"IntegrationException: Problem (Multiple-step operation generated errors. Check each status value.)"
Imports ADODB
Dim _RecordSet As Recordset
_rs.Fields.Append("Field_Name", DataTypeEnum.adVarChar, 50)
_Recordset("Field_Name").Value = _RecordDetails.Field_NameValue
_RecordDetails.Field_NameValue length was more than 50 chars , so this property was violated , hence the error occurred , so you should probably check if you didn't match one of the properties
I am getting this specific error, help would be appreciated
Microsoft VBScript runtime error '800a01a8'
Object required: 'openRecordSet(...)'
/admin/users/affiliates/process.asp, line 47
Line 47 is Set objRecordset = openRecordset(strSQL, objConnection)
<%
SetUserLevel(" 2 ")
If (InStr(Request.ServerVariables("HTTP_REFERER"), "://jim/admin/users/affiliate") = 0) Then
Response.Redirect( "/admin/users/affiliate/" )
End If
Dim objConnection, objRecordset, strSQL, Affiliate_ID
If (IsEmpty(Request.Form("Affiliate_ID")) Or RTrim(Request.Form("Affiliate_ID")) = "") Then
Affiliate_ID = 0
Else
Affiliate_ID = prepareSQL(Request.Form("Affiliate_ID"))
End If
strSQL = "EXEC sp_User_Add_Affiliate " & _
Session("User_ID") & ", '" & _
prepareSQL(Request.Form("First_Name")) & "', '" & _
prepareSQL(Request.Form("Middle_Initial")) & "', '" & _
prepareSQL(Request.Form("Last_Name")) & "', '" & _
prepareSQL(Request.Form("Email_Address")) & "', '" & _
Request.ServerVariables("REMOTE_ADDR") & "', " & _
Session.SessionID & ", '" & _
prepareSQL(Request.Form("Address_1")) & "', '" & _
prepareSQL(Request.Form("Address_2")) & "', '" & _
prepareSQL(Request.Form("City")) & "', '" & _
prepareSQL(Request.Form("State")) & "', '" & _
prepareSQL(Request.Form("Zip")) & "', '" & _
prepareSQL(Request.Form("Country")) & "', '" & _
prepareSQL(Request.Form("Phone")) & "', '" & _
prepareSQL(Request.Form("Phone_Extension")) & "', '" & _
prepareSQL(Request.Form("Fax")) & "', '" & _
prepareSQL(Request.Form("Company")) & "', '" & _
prepareSQL(Request.Form("Pay_To")) & "', '" & _
prepareSQL(Request.Form("Tax_ID")) & "', '" & _
prepareSQL(Request.Form("Tax_ID_Type")) & "', '" & _
prepareSQL(Request.Form("Tax_Class")) & "', " & _
Affiliate_ID & "," & _
Request.Form("ID") & "," & _
Request.Form("Approved")
Set objConnection = openConnectionAdmin()
Set objRecordset = openRecordset(strSQL, objConnection)
If objRecordset("Error") = "1" Then
Response.Write objRecordset("Data")
Response.End
End If
objRecordset.Close
Set objRecordset = Nothing
Set objConnection = Nothing
Response.Redirect ( "/admin/users/affiliates/" ) %>
Function openRecordSet(ByVal strSQL, ByRef objConnection)
On Error Resume Next
' logSQL(strSQL)
Set openRecordset = objConnection.Execute(strSQL)
If err.Number <> 0 Then
'Response.Write Err.Number & " - " & Err.Description logError("ASP: openRecordset: " & Err.Number & " - " & Err.Description & ": " & strSQL)
' Call displayErrorPage()
End If
End Function
The error typically is caused by using Set to indicate assignment of an object to a variable, but having a non-object for the right value:
>> Set v = New RegExp
>> [no news here are good news]
>> Set v = "a"
>>
Error Number: 424
Error Description: Object required
So check your openRecordset function. Does it return a recordset by executing
Set openRecordset = ....
(mark the Set) for the given parameters?
Update wrt comments:
This test script:
Option Explicit
' How to test the type of a function's return value that should
' be an object but sometimes isn't. You can't assign the return value
' to a variable because of VBScript's disgusting "Set".
WScript.Echo "TypeName(openConnectionAdmin()): ", TypeName(openConnectionAdmin())
WScript.Echo "TypeName(openRecordset(...)) : ", TypeName(openRecordset("", objConnection))
' Trying to create a connection and a recordset
Dim objConnection : Set objConnection = openConnectionAdmin()
Dim objRecordset : Set objRecordset = openRecordset("", objConnection)
Function openConnectionAdmin()
' Set openConnectionAdmin = CreateObject("ADODB.CONNECTION")
Set openConnectionAdmin = Nothing
End Function
' After removing the comments: Obviously this is a function that
' hides all errors; the programmer should be fed to the lions.
Function openRecordSet(ByVal strSQL, ByRef objConnection)
On Error Resume Next
Set openRecordset = objConnection.Execute(strSQL)
End Function
output:
TypeName(openConnectionAdmin()): Connection
TypeName(openRecordset(...)) : Empty
... Microsoft VBScript runtime error: Object required: 'openRecordset(...)'
or
TypeName(openConnectionAdmin()): Nothing
TypeName(openRecordset(...)) : Empty
... Microsoft VBScript runtime error: Object required: 'openRecordset(...)'
shows: By hiding every conceivable error in openRecordset() the function can return Empty (undetected!), which isn't an object and can't be assigned to a variable by using Set.
I am trying to validate data, and depends on the result checked or unchecked the related check box, but getting below error?
Microsoft VBScript compilation error '800a03ee'
Expected ')'
<%# Language=VBScript %>
<%
Function Select()
Dim apple, banana, true
Dim SQL
SQL = " SELECT code, name, value FROM fruits WHERE code = '" &("myCode")& "'"
If (SQL = SQL & "name <> '" banana "' AND value <> '" true "' ") or (SQL = SQL & "name = '" banana "' AND value <> '" true "' ") Then
checkedBox.Common.Name = "chkbanana"
checkedBox.Checked = false
Else
checkedBox.Common.Name = "chkbanana"
checkedBox.Checked = true
End If
If (SQL = SQL & "name <> '" apple "' AND value <> '" true "' ") or (SQL = SQL & "name = '" apple "' AND value <> '" true "' ") Then
checkedBox.Common.Name = "chkapple"
checkedBox.Checked = false
Else
checkedBox.Common.Name = "chkapple"
checkedBox.Checked = true
End If
End Function
%>
You have a lot of syntax errors. When concatenating strings you need to use & around banana and such. Also, the logic seems off as you are comparing something with itself and concatenating, basically you can get it to compile and never have it run any of your conditionals as the else statements will always be run. You may want to use contains or some other comparison.
But for syntax your first if statement should look like this:
If (SQL = SQL & "name <> '" & banana & "' AND value <> '" & true & "' ") Or (SQL = SQL & "name = '" & banana & "' AND value <> '" & true & "' ") Then
Also you should not use true as the name of a variable.
In my vb6 I am getting error 3704 operation is not allowed when object is closed.
I have search stackoverflow for similar problem but I think I'm missing something. I need to update every row in vfp based from recordset rs1 Here my code:
Option Explicit
Dim cn As New ADODB.Connection
Dim cn1 As New ADODB.Connection
Private Sub trns_Click()
Set cn = New ADODB.Connection
Set cn1 = New ADODB.Connection
cn.ConnectionString = MDI1.txtcn.Text
cn.Open
cn1.ConnectionString = "Provider=VFPOLEDB;Data Source=\\host1\software\MIL\company0"
cn1.Open
rs1.Open "Select * from trans", cn, adOpenStatic, adLockPessimistic
Do While Not rs2.EOF
rs2.Open "update transac set no_ot_1_5 = " & rs1.Fields("ovt1") & ", no_ot_2_0 = " & rs1.Fields("ovt2") & ", no_ot_3_0" _
& "= " & rs1.Fields("ovt3") & ",Meal_allw = " & rs1.Fields("meal_allow") & ",on_duty = " & rs1.Fields("cnt") & ",no_d_local = " & rs1.Fields("local") & ",no_d_sick" _
& "= " & rs1.Fields("sick") & ",no_d_abs = " & rs1.Fields("absence") & ",no_d_spc = " & rs1.Fields("special") & ",Revenue02" _
& "= " & rs1.Fields("refund") & ",Revenue05 = " & rs1.Fields("prepay") & ",Deduct05 = " & rs1.Fields("prepay") & ",Revenue01 = " & rs1.Fields("comm") & "where code = '" & rs1.Fields("emp_code") & "' and transac.date = CTOD('" & trans.txtend2 & "')", cn1, adOpenDynamic, adLockPessimistic
If Not rs2.EOF Then
rs2.MoveNext
End If
Loop
rs2.close
Update query doesn't return recordset, hence your rs2 is not opened.
You perform your loop on the wrong recordeset : I replaced the some of the rs2 with rs1 in your code.
Do While Not rs1.EOF
rs2.Open "update transac set no_ot_1_5 = " & rs1.Fields("ovt1") & ", no_ot_2_0 = " & rs1.Fields("ovt2") & ", no_ot_3_0" _
& "= " & rs1.Fields("ovt3") & ",Meal_allw = " & rs1.Fields("meal_allow") & ",on_duty = " & rs1.Fields("cnt") & ",no_d_local = " & rs1.Fields("local") & ",no_d_sick" _
& "= " & rs1.Fields("sick") & ",no_d_abs = " & rs1.Fields("absence") & ",no_d_spc = " & rs1.Fields("special") & ",Revenue02" _
& "= " & rs1.Fields("refund") & ",Revenue05 = " & rs1.Fields("prepay") & ",Deduct05 = " & rs1.Fields("prepay") & ",Revenue01 = " & rs1.Fields("comm") & "where code = '" & rs1.Fields("emp_code") & "' and transac.date = CTOD('" & trans.txtend2 & "')", cn1, adOpenDynamic, adLockPessimistic
If Not rs1.EOF Then
rs1.MoveNext
End If
Loop
rs1.close
You dont need to create a recordset to execute an update, insert or delete on the database. Just use the statement cn1.Execute YourSqlStatement where YourSqlStatement is the string you are passing on the rs2.Open instruction. The Execute method on the connection optionally accepts a byRef variable where you can get the number of records affected.
Example:
Dim nRecords As Integer
cn1.Execute "Update Table Set Field = Value Where AnotherField = SomeValue ", nRecords
MsgBox "Total Updated Records: " & Format(nRecords,"0")
try to open your rs2 before using if in the do while statement., or do it like this
rs2.open " blah blah blah "
Do Until rs2.eof
For Each fld In rs2.field
value_holder = fld.value
Next
rs2.movenext
Loop