Private Sub Form_Activate()
Dim st1 As String
'if txtmode 1 fetch record of id from database
If txtmode.Text = "1" Then
'SQL statement
openCon
st1 = "SELECT Customer_name, Address1, Address2, City, Contact FROM customer WHERE id=" & txtid.Text
recSet.Open st1, conn, adOpenDynamic, adLockOptimistic
recSet.MoveFirst
If recSet.Fields("Customer_name").Value <> vbNullString Then
txtCustomer_name = recSet.Fields("Customer_name").Value
Else
txtCustomer_name = ""
End If
When I run my program, I get an error:
compiler error : invalid use of property on txtCustomer_name = line
Why? and how can I solve it ?
You can try this:
If IsNull(recSet.Fields("Customer_name").Value) Then
txtCustomer_name.Text = ""
Else
txtCustomer_name.Text = recSet.Fields("Customer_name").Value
End If
Related
I have made lines of code as seen :
`
Public Class frm_login_a188500
Private Sub btn_login_Click(sender As Object, e As EventArgs) Handles btn_login.Click
Dim mysql As String = "SELECT COUNT( * ) AS NUM_MATCHES FROM TBL_USERS_A188500 WHERE FLD_USERNAME ='" & txt_username.Text & "’ and FLD_PASSWORD ='" &
txt_password.Text & "’"
Dim mydatatable As New DataTable
Dim myreader As New OleDb.OleDbDataAdapter(mysql, myconnection)
myreader.Fill(mydatatable)
Dim num_matches As String = mydatatable.Rows(0).Item("NUM_MATCHES")
If num_matches = 1 Then
frm_mainmenu_a188500.Show()
Me.Hide()
Else
txt_username.Text = ""
txt_password.Text = ""
MsgBox("Incorrect Username or Password")
End If
End Sub
End Class
`
However, there's an error :
System.Data.OleDb.OleDbException: 'Syntax error (missing operator) in query expression 'FLD_USERNAME ='’ and FLD_PASSWORD ='’'.'
Expecting to get the error so that I can fix it.
Don't use smart quotes:
Dim mysql As String = "SELECT COUNT(*) AS NUM_MATCHES FROM TBL_USERS_A188500 WHERE FLD_USERNAME ='" & txt_username.Text & "' And FLD_PASSWORD ='" & txt_password.Text & "'"
I'm new to Vb6.
I haven't inserted data in my database and when I run the program, an error shows No Value given for one or more required parameters
Here's the code:
Dim list As ListItem, r As Integer
If recset.State = adStateOpen Then recset.Close
recset.Open "SELECT StudentId, LastName, FirstName, MiddleName FROM Students ORDER BY StudentId", rainCon, adOpenStatic, adLockOptimistic
If recset.RecordCount > 1 Then
MsgBox "No Data Found!", vbInformation, ""
Else
ListView1.ListItems.Clear
Do While Not recset.EOF
r = r + 1
Set list = ListView1.ListItems.Add(, , r)
list.SubItems(1) = recset(0).Value
list.SubItems(2) = recset(1).Value
list.SubItems(3) = recset(2).Value
list.SubItems(4) = recset(3).Value
recset.MoveNext
Loop
End If
Then highlights recset.Open part. How to control or fix this error?
When I tried to re-check my code and database multiple times. I found out that there was a missing attribute in my students table. The code was fully functional.
Find out the full working code.
Private Sub cmdAddItemtoListBox_Click()
'Enter Code here....
End Sub
Change database path, name, and table as per your data.
Output Screenshot
Dim xCon As New ADODB.Connection
Dim rsTable As New ADODB.Recordset
Dim StrSqlQuery As String
Dim Counter As Integer
Dim list As ListItem
If xCon.State = 1 Then xCon.Close
xCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\Test.mdb;Persist Security Info=False"
'
StrSqlQuery = ""
StrSqlQuery = "Select * from Data Order by PrintSl"
If rsTable.State = 1 Then rsTable.Close
rsTable.CursorLocation = adUseClient
rsTable.Open StrSqlQuery, xCon, adOpenStatic, adLockReadOnly
If rsTable.RecordCount > 0 Then
rsTable.MoveFirst
ListView1.ListItems.Clear
ListView1.ColumnHeaders.Add , , "ID"
ListView1.ColumnHeaders.Add , , "Customer Name"
ListView1.ColumnHeaders.Add , , "Address"
ListView1.ColumnHeaders.Add , , "Pincode"
ListView1.ColumnHeaders.Add , , "Amount"
Do While Not rsTable.EOF
Counter = ListView1.ListItems.Count + 1
Set list = ListView1.ListItems.Add(, , Counter)
list.ListSubItems.Add , , rsTable!CUSTOMERNAME
list.ListSubItems.Add , , rsTable!ADDRESS1
list.ListSubItems.Add , , rsTable!pin
list.ListSubItems.Add , , rsTable!LOAN_AMOUNT
rsTable.MoveNext
Loop
Else
MsgBox "No record found in the table.", vbCritical
End If
If rsTable.State = 1 Then rsTable.Close
If xCon.State = 1 Then xCon.Close
MsgBox "Record Add Successfully", vbInformation
when I m trying to set Recordset using oracle connection string, I m getting OUt of memory error.
on line "rsLIS.Open sql, gConnLIS, adOpenStatic, adLockReadOnly"
However some time it works like once in 5-6 attempts
but when it works it gives error on some other line
on line "If rsLink.Fields(2).value = rsLIS.Fields(1).value Then"
here are the things which I tried :
instead of directly using recordset, I tried to create array (GetRows) method.
Even though recordset count is 26 but UBound of array is showing 1
I have trying changing 3rd argument value from static to forward only
in line ""rsLIS.Open sql, gConnLIS, adOpenStatic, adLockReadOnly""
it also didn't work, it was showing recordset.count as 0
Did try after restarting the client system still same
I m getting this error on client side and since at my place I don't have development environment to debug
Error is "OUT OF MEMORY"
Public Function GetResults_New(MachName As String, patid As String, bCheckDate As Boolean, SendAssay As Boolean) As ADODB.Recordset
On Error GoTo errdesc
Call ShowTempMsg("Line 1")
Dim bXVar As Boolean
Dim i, j As Integer
Dim tmplis, tmporder
Dim tmpresult
bXVar = False
Dim rec_result As New ADODB.Recordset
Dim rsLink As New ADODB.Recordset
Dim rsLIS As New ADODB.Recordset
Dim xSampleIdType As String
gAppPath = AddEditINIfile("VAHSIF.INI", "IF", "sLinkPath", "")
xSampleIdType = AddEditINIfile(gAppPath & "\sLinkConfig.ini", MachName, "SampleIdType", "SampleId1", False)
Call Open_Connection
Call Open_Connection_LIS
rec_result.CursorLocation = adUseClient
If SendAssay = True Then
rec_result.Fields.Append "machineparamid", adBSTR, 50
rec_result.Fields.Append "Assayno", adBSTR, 50
rec_result.Fields.Append "SType", adBSTR, 50
rec_result.Fields.Append "Dilution", adBSTR, 50
Else
rec_result.Fields.Append "machineparamid", adBSTR, 50
rec_result.Fields.Append "SType", adBSTR, 50
rec_result.Fields.Append "Dilution", adBSTR, 50
End If
rec_result.Open
\
'Link Query For Mapped Params.
sql = "SELECT EquipParamMapping.EquipId, EquipParamMapping.EquipParamCode, EquipParamMapping.LISParamCode, EquipParamMapping.EquipAssayNo from EquipParam, EquipParamMapping where equipParam.equipid = equipparammapping.equipid and equipparam.equipparamcode = equipparammapping.equipparamcode and EquipParam.EquipID = '" & MachName & "' and EquipParam.isProgram = 'Y'"
**rsLink.Open sql, gConn, adOpenStatic, adLockReadOnly**
If enumConnTo = connOracle Then
sql = "select " & xSampleIdType & " , LIS_Param_Code From SL_21CI_View_sampleid_Orders where " & xSampleIdType & " || SuffixCode = '" & patid & "' and isApplicable <> 'N' "
Else
sql = "select " & xSampleIdType & " , LIS_Param_Code From SL_21CI_View_sampleid_Orders where " & xSampleIdType & " + cast(SuffixCode as varchar(20)) = '" & patid & "' and isApplicable <> 'N' "
End If
rsLIS.Open sql, gConnLIS, adOpenStatic, adLockReadOnly
While Not rsLIS.EOF
If bXVar = True Then
rsLink.MoveFirst
bXVar = False
End If
While Not rsLink.EOF
bXVar = True
**If rsLink.Fields(2).value = rsLIS.Fields(1).value Then**
If SendAssay = True Then
rec_result.AddNew
rec_result("machineparamid") = rsLink.Fields("EquipParamCode")
rec_result("Assayno") = rsLink.Fields("EquipAssayNo")
rec_result("SType") = " "
rec_result("Dilution") = "0"
rec_result.Update
rec_result.MoveFirst
Else
rec_result.AddNew
rec_result("machineparamid") = rsLink.Fields("EquipParamCode")
rec_result("SType") = " "
rec_result("Dilution") = "0"
rec_result.Update
rec_result.MoveFirst
End If
GoTo NextParam
End If
rsLink.MoveNext
Wend
NextParam:
rsLIS.MoveNext
Wend
Set GetResults_New = rec_result
Exit Function
errdesc:
Call InsertIntoLogWithFileName("Transaction.GetResults_New" & vbNewLine & sql & vbNewLine & err.Description & "ErrLine : " & ErrLine)
End Function
Thanks
That still leaves the question on which line the error occurs. Also: "it also didn't work, it was showing recordset.count as 0". The RecordSet.Count property depends on the provider. Use a function similar to this instead:
Public Function RecordCount(ByVal cn As ADODB.Connection, ByVal sTable As String) As Long
Dim sSQL As String, lRetVal as Long
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
sSQL = "SELECT COUNT(1) AS RecCount FROM " & sTable & ";"
Call rs.Open(sSQL, cn)
If Not (rs.BOF And rs.EOF) Then
lRetVal = rs.Fields("RecCount").Value
Else
lRetVal = -1
End If
Call rs.Close
Set rs = Nothing
RecordCount = lRetVal
End Function
The .Count property might also very well be the cause of the Out of memory error, as I seem to remember that for determing the number of records, it loads all records (from the server) to count them. But I might be wrong there.
Dim srch As String
srch = ccode.Text
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + IO.Directory.GetCurrentDirectory + "\ptdr.accdb"
cmd.Connection = conn
conn.Open()
Dim dsrch As New OleDbCommand("SELECT pname, page, vdate, psex, summer, bldp, blds, photh, diag, rl, trtmnt, nvdate FROM ptnt_data WHERE pcode = " & srch & "", conn)
Dim rdr As OleDbDataReader = dsrch.ExecuteReader()
Dim dg As String = ""
'diagnosis'
dg = rdr.GetString(8).ToString()
If String.IsNullOrEmpty(dg) Then
diag1.Text = ""
ElseIf String.IsNullOrWhiteSpace(dg) Then
diag1.Text = ""
Else
diag1.Text = dg
End If
It works fine but when I search for a null it gives an error.
It says that I've an error at the line of
dg = rdr.GetString(8).Tostring()
any suggests?
rdr.GetString(8).ToString()
if rdr.GetString(8) is null this is a classic null reference exception
rdr.GetString(8) IS a string so just remove the .ToString()
As per my comment below you can only run GetString if the DB has an underlying type of string for this data, what is the datatype of column diag?
(ie run rdr.GetFieldType(8) in the debugger and provide the type name)
As you have confirmed this is a string type and the issue is just with null handling (sorry i didn;t spot that from your question) you need:
If rdr.IsDBNull(8) Then diag1.Text = "" Else diag1.Text = rdr.GetString(8) EndIf
Which should replace everything from Dim dg... down
How to return the value from function
Code
Private Function LeaveCheck(empid As String)
Dim rdoRs1 As rdoResultset
Dim desc As String
Dim sSQL As String
sSQL = "Select name from table1 wher empcode = '" & empid & "'"
Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)
If rdoRs1.RowCount > 0 Then
desc = rdors1!name
return desc 'Showing error in this line
End If
rdoRs1.Close
End Function
How to return the value from the above code.
Need Vb6 code Help
You need to specify your return type.
Private Function LeaveCheck(empid As String) As String ' Notice the As String
Dim rdoRs1 As rdoResultset
Dim desc As String
Dim sSQL As String
sSQL = "Select name from table1 wher empcode = '" & empid & "'"
Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)
If rdoRs1.RowCount > 0 Then
desc = rdors1!name
End If
rdoRs1.Close
LeaveCheck = desc ' This will be blank or populated
End Function
Here is a link that is a good read for understanding Functions in VB6
EDIT
After reading your comment, I would create a class to store your values.
Public Class MyClass
Dim name As String
Dim dept As String
Dim country As String
End Class
Then, you can instantiate a new instance of this class inside of your code:
Private Function LeaveCheck(empid As String) As MyClass
Dim myClass As New MyClass
Dim rdoRs1 As rdoResultset
Dim sSQL As String
sSQL = "Select name, dept, country from table1 wher empcode = '" & empid & "'"
Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)
If rdoRs1.RowCount > 0 Then
myClass.name = rdors1!name
myClass.dept = rdors1!dept
myClass.country = rdors1!country
End If
rdoRs1.Close
LeaveCheck = myClass
End Function
You need to set the value to the function name and the return type:
Private Function LeaveCheck(empid As String) As String
Dim rdoRs1 As rdoResultset
Dim desc As String
Dim sSQL As String
sSQL = "Select name from table1 wher empcode = '" & empid & "'"
Set rdoRs1 = Rdoconn.OpenResultset(sSQL, rdOpenStatic)
If rdoRs1.RowCount > 0 Then
desc = rdors1!name
End If
rdoRs1.Close
LeaveCheck = desc
End Function
See this document for more information.
Function returnArray() As Variant
RTA[ab]=0
if a=b then RTA[ab]=1
RTA[xy]=0
if a=b then RTA[xy]=1
returnArray=RTA
end function