How to read dt_DBTIME2 with VBScript - vbscript

I'm trying to read a recordset from SQL database were some field are of type time(n).
When I read a row (recordset.Fields(n)), VBScript stops at this field with a 'multiple step error'.
I tried to set a variable with the recordset, so:
Dim a
Set a = recordset.Fields(specific time column)
I tried to cast the field.
I tried to find any information about this specific type, but I can't find any information about this time structure, besides it contains hour, minute, second and fractional parts.
ReDim iArr(iColumnCount)
While Not objRecordset.EOF
For iColumnIndex = 0 To (iColumnCount-1)
'SQL_FIELDTYPE_TIME is a constant at top of page with value 145
If objRecordset.Fields(iColumnIndex).Type = SQL_FIELDTYPE_TIME Then
'Tried so much over here...
Dim Tme, Str
Set Tme = objRecordset.Fields.Item(iColumnIndex)
iArr(iColumnIndex) = CStr(clDBTime(Tme).Hour)
Set Tme = Nothing
Else
iArr(iColumnIndex) = objRecordset.Fields.Item(iColumnIndex)
End If
If Err.Number <> 0 Then
fLogData("Ophalen data (" + oDB.strDatabaseNaam + ") rij " + _
CStr(objRecordset.AbsolutePosition) + " kolom " + _
CStr(iColumnIndex) + " mislukt: " + Err.Description)
Err.Clear
End If
Next
List.Add i, iArr
i = i + 1
objRecordset.MoveNext 'Next row
Wend
Errors I'm getting:
Object doesn't support this property
Multiple step error
Type mismatch
etc. etc.
Anyone who can help me out how to read the time value from SQL in VBScript?
EDIT: solved! Cost almost 4h trial-error but there's a solution:)
Changed the SQL select statement to:
SELECT COL, COL,... (this are the 'usual' columns),CAST(TIMECOL -this is the time(n) column- as varchar),CAST(TIMECOL2 -this is the time(n) column- as varchar) FROM tabel
And within code: TimeValue(read value - from recordset.fields...) (this is to change varchar back to time)
And .. it just works great!
Edit2: recordset code:
if iResCount > 0 and iColumnCount > 0 then
redim iArr(iColumnCount)
while not objRecordset.EOF
for iColumnIndex = 0 to (iColumnCount-1)
iArr(iColumnIndex) = objRecordset.Fields.Item(iColumnIndex)
if Err.Number <> 0 then
fLogData("Ophalen data ("+oDB.strDatabaseNaam+") rij "+cstr(objRecordset.AbsolutePosition)+ " kolom "+cstr(iColumnIndex)+" mislukt: "+Err.Description)
Err.Clear
end if
next
List.Add i, iArr
i = i + 1
objRecordset.MoveNext 'Next row
wend
set oResult = List'CreateObject("Scripting.Dictionary")
end if
And an example of a function call:
'fGetRecord(DB, Table, Columns, Condition, Field to sort on, Type of sort)
fGetRecord(oHoofdDB,CONST_HDB_STR_TBL_KALENDER,CONST_HDB_STR_KAL_ID+","+CONST_HDB_STR_KAL_NAAM+",CAST("+CONST_HDB_STR_KAL_STARTIJD+" as varchar),CAST("+CONST_HDB_STR_KAL_STOPTIJD+" as varchar)",CONST_HDB_STR_KAL_NAAM+"='"+Naam+"'", "", SORTEER_OPLOPEND)
John

Related

How do I find a repeating set of cells in Excel?

I Have a 2100 Rows and 6 Columns Table
Throughout the table there are only 12 Possible values, say A,B,C,D,E,F,G,H,I,J,K,L
The 12th value L is just a blank filler. It denotes blank cell.
Since there are only 11 possible values througout the table, patterns are observed.
First a Pattern Appears and it is later repeated somewhere in the table.
There can be any number of Patterns, but i have a specific format for a pattern which is to found and reported that way.
Solutions in EXCEL-VBA, PHP-MYSQL or C are welcome.
I have attached an example of what Iam looking for. Suggestions are most welcome to refine
the questions.
Information & Format : http://ge.tt/8QkQJet1/v/0 [ DOCX File 234 KB ]
Example in Excel Sheet : http://ge.tt/69htuNt1/v/0 [ XLSX File 16 KB ]
Please comment for more information or specific requirement.
Please try the code below, change the range to what you need it to be and the sheet number to the correct sheet number (I wouldn't put your full range in just yet because if you have 1000 pattern finds, you'll have to click OK on the message box 1000 times, just test with a partial range)
This will scan through the range, and find any pattern of two within a 10 row range, if you need it to find bigger patterns, youll need to add the same code again with an extra IF statement checking the next offset.
This will only find it if the same pattern exists and the same column structure is present, but its a start for you
Works fine on testing
Sub test10()
Dim rCell As Range
Dim rRng As Range
Set rRng = Sheets("Sheet1").Range("A1:I60") '-1 on column due to offset
'Scan through all cells in range and find pattern
For Each rCell In rRng.Cells
If rCell.Value = "" Then GoTo skip
For i = 1 To 10
If rCell.Value = rCell.Offset(i, 0).Value Then
If rCell.Offset(0, 1).Value = rCell.Offset(i, 1) Then
MsgBox "Match Found at: " & rCell.Address & ":" & rCell.Offset(0, 1).Address & " and " & rCell.Offset(i, 0).Address & ":" & rCell.Offset(i, 1).Address
End If
End If
Next i
skip:
Next rCell
End Sub
***UPDATE***
I have updated my code, the following now finds the pattern wherever it may appear in the next 10 rows:
Sub test10()
Dim rCell As Range
Dim rRng As Range
Dim r1 As Range
Dim r2 As Range
Set rRng = Sheets("Sheet1").Range("A1:I50") '-1 on column due to offset
i = 1 'row length
y = 0 'column length
'Scan through all cells in range and find pattern
For Each rCell In rRng.Cells
If rCell.Value = "" Then GoTo skip
i = 1
Do Until i = 10
y = 0
Do Until y = 10
xcell = rCell.Value & rCell.Offset(0, 1).Value
Set r1 = Range(rCell, rCell.Offset(0, 1))
r1.Select
ycell = rCell.Offset(i, y).Value & rCell.Offset(i, y + 1).Value
Set r2 = Range(rCell.Offset(i, y), rCell.Offset(i, y + 1))
If ycell = xcell Then
Union(r1, r2).Font.Bold = True
Union(r1, r2).Font.Italic = True
Union(r1, r2).Font.Color = &HFF&
MsgBox "Match Found at: " & rCell.Address & ":" & rCell.Offset(0, 1).Address & " and " & rCell.Offset(i, y).Address & ":" & rCell.Offset(i, y + 1).Address
Union(r1, r2).Font.Bold = False
Union(r1, r2).Font.Italic = False
Union(r1, r2).Font.Color = &H0&
End If
y = y + 1
Loop
i = i + 1
Loop
skip:
Next rCell
End Sub

recordset.close got wrong help plssss

Help my code got wrong it says:
Either BOF or EOF is True, or the current record has been deleted,Requested operation requires a current record.
i think this rs.close has a problem or where can i set the rs.close? because i set and used recordset twice.
anyone can help? please fix my code.
Public Function borrowersName(ByVal Iname, ByVal Imod, ByVal Icat, ByRef BFname, ByRef BLname) As Boolean
Dim dateReturned As String
'select firt the primary key of the item
qry1 = "select tblitem_id from tblitem inner join tblcategory on tblitem.tblcategory_id=tblcategory.tblcategory_id where tblitem.item_name='" + Iname + "' and tblitem.item_model='" + Imod + "' and tblcategory.category_name='" + Icat + "'"
rs.Open qry1, conn
qry1Result = rs.Fields(0).Value
rs.Close
qry2 = "SELECT date_returned,Firstname,Lastname FROM tblborrowers where tblitem_id='" & qry1Result & "' ORDER BY tblborrowers_id DESC LIMIT 1"
rs.Open qry2, conn
dateReturned = rs.Fields(0).Value
If dateReturned <> "" Then
borrowersName = True
BFname = rs.Fields(1).Value
BLname = rs.Fields(2).Value
Else
borrowersName = False
End If
Set rs = Nothing
End Function
You do have the recordset Open and Close methods in the right order, so there is no problem there.
The error "Either BOF or EOF is True, or the current record has been deleted" simply means that one of your SELECT queries has returned zero records. What you do about that depends on your requirements. For example, you could test for Not rs.EOF before attempting to read a field value.

How to check listindex of combo in vb6?

I load values of combo boxes here but I don't want to set ListIndex property to -1.
Private Sub Form_Load()
OPENCON
RES.Open "DIVISION", CON, adOpenDynamic, adLockOptimistic, adCmdTable
If RES.RecordCount > 0 Then
RES.MoveFirst
For i = 0 To RES.RecordCount - 1
CmbDiv.AddItem RES.Fields("DIV").Value
CmbDiv.ItemData(CmbDiv.NewIndex) = RES.Fields("DIVID").Value
RES.MoveNext
Next
End If
RES.Open "HNM", CON, adOpenDynamic, adLockOptimistic, adCmdTable
If RES.RecordCount > 0 Then
RES.MoveFirst
For i = 0 To RES.RecordCount - 1
CmbHouse.AddItem Trim(RES.Fields("HOUSE").Value)
CmbHouse.ItemData(CmbHouse.NewIndex) = RES.Fields("HID").Value
RES.MoveNext
Next
End If
End Sub
This is the code I used to modify record.
Private Sub CmdSave_Click()
sql = "UPDATE STUDENT_RECORD_DATABASE SET "
sql = sql + "ROLLNO= " & Val(CmbRNO) & ","
sql = sql + "DIVID='" & Val(CmbDiv.ItemData(CmbDiv.ListIndex)) & "',"
sql = sql + "HID=" & Val(CmbHouse.ItemData(CmbHouse.ListIndex)) & " "
sql = sql + "WHERE ROLLNO= " & Val(CmbRNO) & ""
Set RES = CON.Execute(sql)
End Sub
While running the code if I modify both division and house then it's ok
but when I let any one value of them(doesn't modify value) it shows error on the 3rd and 4th lines of CmdSave_Click:
Invalid Property array value
after loading your data into the combobox, set the listindex to 0
Combo1.ListIndex = 0
it would be better though to check if listindex is -1, and if it is -1, then don't do the action, or give the user a warning that he should select something from the combobox

How to save the listview subitem to database

I want to save the listview subitem in table
Code
Dim sSQL As String
Dim idx As Integer
idx = 1
With lstLVDetails.ListItems
For idx = idx To .count
sSQL = "Insert into sample values ('" & .Item(idx) & "', '" & .Item(idx).SubItems(1) & "') "
RdoVisPay.Execute sSQL, rdExecDirect
Next idx
End
The above code is not showing any error, but it is not inserting any values.
What wrong in my code.
Need code help...
Put a breakpoint on:
RdoVisPay.Execute sSQL, rdExecDirect
Is this line ever getting hit? At the start, idx = 1. if lstLVDetails.ListItems.Count=1, you'll never hit the code to insert anything.

VB6: Runtime Error '13': Type Mismatch when setting and int with an int

I'm not new to VB6 programming, but I'm not a master at it either. Hopefully someone can help me out with a question that I have concerning a Type Mismatch error that I'm receiving from trying to set an int variable with a int returned from a function.
The integer that I'm trying to set is defined as:
Global AICROSSDOCKStatus As Integer
Now when I try to make this call I get the Runtime Error 13
AICROSSDOCKStatus = ProcessQuery(iocode, pb, AICROSSDOCBOLFN, "")
I've stepped through debugging the program line for line. The ProcessQuery function gets and returns the expected integer, but when the assignment is to be made to the AICROSSDOCKStatus it fails.
On a side note, I've also tried doing a CInt() to the ProcessQuery with the same result.
Does anyone have any suggestions for what I may be able to try?
Edit:
Here is the definition of ProcessQuery
Function ProcessQuery(icode As Integer, pb As ADODB.Recordset, TableName As String, sql$) As Integer
Edit 2: I couldn't tell you why this was done in this manner. I inherited the code base. Yikes...
Function ProcessQuery(icode As Integer, pb As ADODB.Recordset, TableName As String, sql$) As Integer
ProcessQuery = ProcessQuery1(icode, pb, TableName, sql$)
End Function
Function ProcessQuery1(icode As Integer, pb As ADODB.Recordset, TableName As String, sql$) As Integer
''THIS IS THE ORIGINAL SQL CALL ROUTINE!
Dim STATUS As Integer
On Error GoTo ProcessSQLError
STATUS = 0
Select Case icode
Case BCLOSE
If pb.State 0 Then
pb.Close
End If
Set pb = Nothing
STATUS = 3
Case BOPEN
STATUS = 9
Set pb = New ADODB.Recordset
Case BOPENRO
STATUS = 9
Set pb = New ADODB.Recordset
Case BGETEQUAL, BGETEQUAL + S_NOWAIT_LOCK, BGETGREATEROREQUAL, BGETGREATEROREQUAL + S_NOWAIT_LOCK
If pb.State 0 Then
pb.Close
''Set pb = Nothing
''Set pb = New ADODB.Recordset
End If
pb.Open sql$, MISCO_SQL_DB, adOpenDynamic, adLockOptimistic
If Not pb.EOF Then
pb.MoveFirst
Else
STATUS = 9
End If
Case BGET_LE, BGET_LE + S_NOWAIT_LOCK
If pb.State 0 Then
pb.Close
End If
pb.Open sql$, MISCO_SQL_DB, adOpenDynamic, adLockOptimistic
If Not pb.BOF Then
pb.MoveLast
Else
STATUS = 9
End If
Case BGETFIRST, BGETFIRST + S_NOWAIT_LOCK
If pb.State 0 Then pb.Close
sql = "select * from " + TableName
If InStr(1, gblOrderBy, "ORDER BY") > 0 Then
sql = sql + gblOrderBy
Else
sql = sql + " ORDER BY " + gblOrderBy
End If
gblOrderBy = ""
pb.Open sql$, MISCO_SQL_DB, adOpenDynamic, adLockOptimistic
If Not pb.EOF Then
pb.MoveFirst
End If
Case BGETLAST, BGETLAST + S_NOWAIT_LOCK
If pb.State 0 Then
pb.Close
End If
sql = "select * from " + TableName
If InStr(1, gblOrderBy, "ORDER BY") > 0 Then
sql = sql + gblOrderBy
Else
sql = sql + " ORDER BY " + gblOrderBy
End If
gblOrderBy = ""
pb.Open sql$, MISCO_SQL_DB, adOpenDynamic, adLockOptimistic
If Not pb.EOF Then
pb.MoveFirst
pb.MoveLast
End If
Case BGETNEXT, BGETNEXT + S_NOWAIT_LOCK: pb.MoveNext
Case BGETPREVIOUS, BGETPREVIOUS + S_NOWAIT_LOCK: pb.MovePrevious
Case B_UNLOCK
''need to add code here
Case BINSERT
If pb.State = 0 Then
pb.Open TableName, MISCO_SQL_DB, adOpenDynamic, adLockOptimistic
End If
Case BDELETE
STATUS = 8
pb.Delete
Case Else
STATUS = 1
MsgBox "STOP: UNDEFINDED PROCEDURE" + Str$(icode)
End Select
If STATUS = 0 Then
If pb.EOF Or pb.BOF Then STATUS = 9
End If
ProcessQuery1 = STATUS
Exit Function
ProcessSQLError:
MsgBox TableName + ": " + Error(Err), vbCritical, "Error "
ProcessQuery1 = 9
End Function
Well, I can't say I know what's breaking, but here's a possible debugging step: Assign a locally defined integer first, then assign AICROSSDOCKStatus to the local int. If the run-time err 13 happens at the first assignment, something REALLY weird is going on - if it happens at the second, then you might want to see if any of your global variables are arrays that you might be overrunning bounds on.
Good luck!
Try using an intermediate Variant which should take any kind of type from your function. It should at least allow you to investigate the real type of the return value.
This will tell you the name of the type your function is actually returning:
MsgBox TypeName(ProcessQuery(...))
Confining the ProcessQuery() function to int might be helpful in any case, to prevent such errors right from the start:
Function ProcessQuery(whatever arguments) As Integer
''// ...
End Function
It's a little unusual for an API to return a VB6 Integer, which is a 16-bit quantity. Fairly often, someone translating a C function prototype will confuse a c "int" with a VB6 Integer, when the equivalent is really a Long in VB6.
The problem was not in the fact that it was having trouble with the returns from a function but the parameters that were being passed by reference. I needed a Recordset(pb) to pass by reference, but I had declared (pb) as a Record.
Try the following and see if the value turns out to be an int,
msgbox ProcessQuery(iocode, pb, AICROSSDOCBOLFN, "")
I am sure it won't be an int & that is the result it is failing. My guess is that, it will have come characters which makes it a non-numeric value.
You can try IsNumeric() function on the results to check, if it is a numeric value.

Resources