Classic ASP Iterate through and Object - vbscript

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

Related

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.

MS Access underline text in a Table

I have long Text field in a table in MS ACCESS. I need to underline it for specific text in a field. I tried to change Text format to Rich Text in the design view , but I am getting:
Error : Operation is not supported for this type of Object
In the table I have 320 rows. I need to Underline for N.J.S.A. only in the long text.
Please help me regarding this. Thanks in Advance
Well... you have to get that field property changed from plain text to rich text for this to work and design view should handle this. If not try the below code.
Public Sub TestUnderline()
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As Field
Dim rst As DAO.Recordset
Dim strSQL As String
Dim strString As String
Set db = CurrentDb
Set tbl = db.TableDefs("Table1") 'Change to your table name
Set fld = tbl.Fields("TestField") 'Change to your field name
With fld.Properties("TextFormat")
If .Value = acTextFormatPlain Then
.Value = acTextFormatHTMLRichText
End If
End With
strSQL = "SELECT TestField " & _ 'Change to your Field name
"FROM Table1;" 'Change to your table name
Set rst = db.OpenRecordset(strSQL)
Do While Not rst.EOF
If InStr(1, rst![TestField], "N.J.S.A") Then 'Change to your field name
strString = Replace(rst![TestField], "N.J.S.A", "<u>N.J.S.A</u>") 'Change to your field name
rst.Edit
rst![TestField] = strString 'Change to your field name
rst.Update
End If
rst.MoveNext
Loop
EndCode:
If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If
If Not tbl Is Nothing Then
Set tbl = Nothing
End If
If Not db Is Nothing Then
Set db = Nothing
End If
End Sub
Credit given to:
How to convert a text field in an Access table to a rich text memo using VBA
and:
http://www.tek-tips.com/viewthread.cfm?qid=1538917

How to populate data in combo box from sql server

I have a combo box name cbSection
how can i populate the data from sql server in combo box ?
Any idea will help
-Thanks
This is direct from VbForums (I take no credit for the code!), first result for a quick Google search.
Dim strSQL as String 'Declare the variables we need
Dim oRS as ADODB.Recordset
Set oRS = New ADODB.Recordset
'Load the data
'** change this SQL to load the data you want.
strSQL = "SELECT Colour FROM Colours"
'** change oConn to the name of your Connection object
oRS.Open strSQL, oConn, adOpenForwardOnly, adLockReadOnly, adCmdText
'Fill the combo box (or ListBox)
'** change the name of the combo to the one you want to fill
With cboColour
.Clear
Do While Not oRS.EOF
'** change the name of the field here to the one you want to show
.AddItem oRS.fields("Colour").value
oRS.MoveNext
Loop
End With
'Tidy up
oRS.Close
Set oRS = Nothing

VB6 module to pass recordset to form

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()

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