VB6 module to pass recordset to form - vb6

lets call it modUser.
In this modUser I have a ADODB record set
Now from this modUser, I would like to open a form. Lets call it frmUser2.
when this frmUser2 initialize. I would like to use the recordset that I already have from modUser. How do I pass this recordset from modUser to frmUser2?
I tried creating a public sub under frmUser2. But I get an error that says "Run time error 13 type mismatch"
here is a snippet
sSQL = "select name from employee"
rs.Open sSQL, ADOCon, adOpenKeyset
If rs.RecordCount > 1 Then
frmUser2.PopulateList(rs)
End if
in frmUser2 I have the public function ( i tried sub too)
Public Function PopulateList(rs As ADODB.Recordset)
For Count = 0 To rs.RecordCount - 1
LstModels.AddItem rs(0)
rs.MoveNext
Next
rs.close
End Function
I tried show , and I can get the form to appear, but I have no way to pass the record set.
frmUser2.Show
Please help. Thank you

I'm not a fan of how you're trying to do this, but working with what you want to do, first create a public Recordset property in your form and assign the recordset to it from your module before showing the form.
Module code:
Dim objForm As frmUser2
sSQL = "select name from employee"
rs.Open sSQL, ADOCon, adOpenKeyset
If rs.RecordCount.EOF = False Then
Set objForm = New frmUser2
frmUser2.Recordset = rs
frmUser2.Show
End if
Form Code:
Private Sub Form_Load()
If Not Recordset Is Nothing Then
PopulateList
End If
End Sub
Public Function PopulateList()
Recordset.MoveFirst 'defensive, make sure we're on the first record
LstModels.Clear
Do While Recordset.EOF = False
LstModels.AddItem Recordset(0)
Recordset.MoveNext
Next
Recordset.Close
End Function
I think it would be preferable for the module to have a public method that returns an employee recordset. Your form would call that method when it needs the data. Set rsEmployees = modUser.GetEmployees()

Related

Recordset not getting updated

I have created a form to allow a user to change their password.
I created a recordset and used edit/update to save it in a query, but the new password is not being saved in the query.
My code is as follows:
Private Sub txtNewPass2_AfterUpdate()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("qryUsers", dbOpenDynaset)
If Me.txtNewPass1 = Me.txtNewPass2 Then
rst.MoveFirst
Do Until rst.EOF
If rst!NName = CboUserName.Column(0) Then
rst.Edit
rst!Password = txtNewPass2.Value
rst.Update
End If
rst.MoveNext
Loop
DoCmd.Openform("frmLogin")
Else: MsgBox "Passwords not Matching"
End If
End Sub
The expression:
DoCmd.Openform("frmLogin")
Will result in a syntax error, as the parentheses surrounding the arguments are not required when the value returned by the function is not used.
However, you may find it cleaner to simply execute a SQL statement to perform the update, rather than iterating over the recordset, i.e.:
Private Sub txtNewPass2_AfterUpdate()
If txtNewPass1 = txtNewPass2 Then
With CurrentDb.CreateQueryDef("", "UPDATE qryUsers SET qryUsers.Password = ?pwd WHERE qryUsers.NName = ?usr")
.Parameters(0) = txtNewPass2
.Parameters(1) = CboUserName.Column(0)
.Execute
End With
DoCmd.Openform "frmLogin"
Else
MsgBox "Passwords not Matching"
End If
End Sub
Using parameters of course to account for users better known as Bobby Tables.

Unspecified error while opening a connection to database

Good eve guys.
I'm having a problem with my code.
The goal is, when I type a letter in the combobox, it wiill show a list of possible model names.
But whenever I type in that combobox, it gives me an error.
Here's the code I'm working on:
Private Sub cmbSearch_Change()
Dim conn As New ADODB.Connection
Dim record As New ADODB.Recordset
Dim model As String
model = cmbSearch.Text
If cmbSearch.Text <> "" Then
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path & "\Database.mdb"
cmbSearch.Clear
record.Open "SELECT Model FROM LaptopSpecs WHERE Model LIKE '" & model & "%' ORDER BY Model", conn, 3, 3
If record.RecordCount > 0 Then
Do While Not record.EOF
cmbSearch.AddItem record.Fields("Model").Value
record.MoveNext
Loop
End If
cmbSearch.Text = model
End If
Set record = Nothing
Set conn = Nothing
End Sub
In the part conn.open, the error message pops up.
run-time error '-2147467259(80004005)': unspecified error
Assuming you are using Access:
This question isn't about Access, but if it where:
Expanding on this answer, I recommend removing all the code in your example, and replacing it with this (done in Form design mode):
Set the cmbSearch.RowSource property to a fixed query like:
SELECT Model FROM LaptopSpecs ORDER BY Model
Set the cmbSearch Auto Expand to True.
This will work for 98% of all ComboBox type-to-select scenarios, and should work for your example.

Classic ASP Iterate through and Object

I'm a PHP developer, learning ASP.
I've become very reliant on PHP's useful functions: print_r() and var_dump() to see what an array or object contains.
I don't always know what columns are in a Db Table. So, when a SELECT * From Tbl is queried, and the objRS is populated, would I be able to view what the entire object's contents are?
Is this possible in ASP?
<% `my simple Select statement
Dim strDbConnection
Dim objConn
Dim objRS
Dim strSQL
strDbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.mdb;"
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open(strDbConnection)
strSQL = "SELECT * FROM persons"
Set objRS = objConn.Execute(strSQL)
If objRS.EOF Then
Response.Write("No items found")
Else
Do While Not objRS.EOF
' show all columns I can extract here....
objRS.MoveNext()
Loop
End If
objRS.Close()
Set objRS = Nothing
objConn.Close()
Set objConn = Nothing
%>
-- I would like to see what's coming back at me in the objRS, and then cherry-pick the columns after I know what I have access to.
Is there something similar to what I'm used to?
ie: print_r()
You can use objRS.fileds(j).name and objRS.fields(j).value to get the name of columns and values.
For example:
[...]
for j = 0 to objRS.fields.count - 1
response.write(objRS.fields(j).name & " = " & objRS.fields(j).value)
next

How to deal with textbox validation?

Private Sub txtUserCode_Validate(Cancel As Boolean)
If RS!ID = txtUserCode.Text Then
SQL = "SELECT NAME,PRIVILEDGE FROM ADMIN WHERE CODE=" & txtUserCode.Text
Set RS = CN.Execute(SQL)
txtUserName.Text = RS!NAME
Else
MsgBox "ENTER VALID NO"
txtUserCode.Text = ""
Cancel = True
End If
End Sub
In this code I want to execute like:
If I enter the ID present in table then it'll show info but it's considering 1st record (RS!ID(0)) only not the next one
If I enter the ID which is not present in table then it should not throw error
3021- Requested operation requires current record but goto else part.
Please Help
I am assuming RS is a recordset.
Depending on the RS type, you can try and Find a record like this:
RS.MoveFirst
RS.Find("[CODE]=" & txtUserCode.Text)
If Not RS.EOF Then
' found!
End If
Link to the ADO Find function.

How can I create datareport using recordset in vb6?

Private Sub showreport_Click()
sql = "select * from student_record_database where"
sql=sql+ Grade='" & Combo1.Text & "' AND Meal='" & Combo11.Text & "'"
Set RES = CON.Execute(sql)
Set DataReport1.DataSource = RES
DataReport1.WindowState = vbMaximized
DataReport1.Show vbModal
End Sub
I am using this code as record set to create a data report.
My task is to choose options from various combo boxes and then display it's report so record set is needed there..
My question is that whether this code is sufficient to create data report???
I didn't set any properties of data environment or data report such as (connection - command - sql) because I am passing this record set directly to data report,then no need to fire any sql in properties of data environment.
But unfortunately it is not showing desired output
Please help me.
Try this one:
Private sub cmdprint_click()
Dim rs as new adodb.recordset
rs.open "SQL Query Statement Here",CON, adOpenDynamic, adLockOptimistic
set datareport1.datasource=rs
datareport1.show
end sub
Notes:
The data report datasouce should be cleared during design mode. (See properties on the datareport and set its datasource property to empty.) Ohhh...one more thing, please keep in mind that you should set also the datafield property for each textbox object inside the datareport corresponding to the datafield on your database during design time...
I am using this method for a long time and it works fine.
Try this.
To add a quite to a string, use a double quite.
Also you missed spelled the second Combo1 reference as Combo11
Private Sub showreport_Click()
sql = "select * from student_record_database where "
sql = sql & "Grade=""" & Combo1.Text & """ AND Meal=""" & Combo1.Text & """"
Set RES = CON.Execute(sql)
Set DataReport1.DataSource = RES
DataReport1.WindowState = vbMaximized
DataReport1.Show vbModal
End Sub

Resources