I am attempting to add columns to an ADO recordset in VB6. I simply want to add 4 columns to the end of the table. It's a table we use constantly but we often delete all the data in it and refill it with the next information we want, basically just using it as a means to temporarily hold data.
I have found that since it is ADO I should be able to use the following:
with rs
.fields.append "column name", "enum dataType"
end with
From reading and experimentation it seems that the recordset has to be closed in order to add the columns.
Here's my code:
rs.Open "MeterReads", DataEnvironment7.cnPTracker, adOpenStatic, adLockOptimistic, adCmdTable
' 2019-11-4 Adding in a section to accomadate for days to depeletion
If gbEnableD2D Then
bExists = False
With rs
For Each fField In rs.Fields
If UCase(fField.Name) = UCase("eddB") Then
bExists = True
Exit For
End If
Next
If bExists = False Then
.Close
.Fields.Append "eddB", adDate
.Fields.Append "eddC", adDate
.Fields.Append "eddM", adDate
.Fields.Append "eddY", adDate
.Open
End If
End With
End If
I would expect there to be additional columns added to my table. However, I can look at the table and see they aren't in there. I can see as I have closed the recordset. That it attempts to append the columns to it. However, I open the recordset back up and those columns disappear from the table. When I say they appear I mean using Microsoft Visual Basic's debug system. It basically has a section where it shows your variables and for objects it shows you the items inside and a bit more info. So I know it attempts it. But I cant seem to get it to retain it. Any thoughts or ideas on were I'm screwing up would be great.
If you want to modify the structure of the table in your database, you can use the Microsoft ADO Ext. 6.0 for DDL and Security library (add it to your Project from the Project menu > References). You can find the table you want to modify by iterating through the Catalog object's Tables collection. First create a Catalog object:
Dim objCatalog As ADOX.Catalog
' Create and Open Catalog
Set objCatalog = New ADOX.Catalog
Set objCatalog.ActiveConnection = DataEnvironment7.cnPTracker
I am assuming DataEnvironment7.cnPTracker is your current ADO Connection object.
Then iterate through Tables:
Dim objTable As ADOX.Table
Dim sTableName As String
sTableName = "Customers"
' Check if Table exists
For Each objTable In objCatalog.Tables
If objTable.Name = sTableName Then
' Table found, return reference
Exit For
End If
Next
Then, once you have the table, you can go through the Columns collection to see if it exists:
Dim objColumn As ADOX.Column
For Each objColumn In objTable.Columns
Finally, if you can't find the column, you can add it:
Set objColumn = New ADOX.Column
With objColumn
.Name = "FieldName"
.DefinedSize = 200
.Type = adVarChar
End With
' Append the new field
objTable.Columns.Append objColumn
One approach would be to add the columns when you open the recordset by modifying how you retrieve the data. Instead of using rs.Open with adCmdTable, use adCmdText with a SELECT statement.
SELECT *, NULL AS eddB, NULL AS eddC, NULL AS eddM, NULL AS eddY FROM MeterReads
with rs
.fields.append "column name", "enum dataType"
end withrs.Open "MeterReads", DataEnvironment7.cnPTracker, adOpenStatic, adLockOptimistic, adCmdTable
' 2019-11-4 Adding in a section to accomadate for days to depeletion
If gbEnableD2D Then
bExists = False
With rs
For Each fField In rs.Fields
If UCase(fField.Name) = UCase("eddB") Then
bExists = True
Exit For
End If
Next
If bExists = False Then
.Close
.Fields.Append "eddB", adDate
.Fields.Append "eddC", adDate
.Fields.Append "eddM", adDate
.Fields.Append "eddY", adDate
.Open
End If
End With
End If
Related
I have a SQL table which is containing some xml data like below -
xmlData
I wrote a VBS code which is extract all the SQL column data excluding "XMLData"
my Dummy VBS Code -
Sub getdata()
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Connection String"
Set objRecordSet = CreateObject("ADODB.Recordset")
sqlquery = "select * from TableName WHERE IsExecute = 'Yes'"
Set rs = objConnection.Execute(sqlquery)
If Recordset.EOF Then
msgbox "There are no records to retrieve; Check that you have the correct job number."
Else
'if there are records then loop through the fields
Do While NOT Recordset.Eof
field = Recordset("xmlData")
if field <> "" then
Msgbox field
end if
Recordset.MoveNext
Loop
End Sub
Need a help to get xml format data from SQL database.
like -
***xmlData
<
EndResync/> ***
Thanks
RaviK
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.
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
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
I have a recordset populated from a stored procedure
Set PesquisaTabela = cmd.Execute
If Not PesquisaTabela.EOF Then
txtUS.Text = PesquisaTabela.Fields(0)
end if
How can I use this recordset to populate an fplist / listbox in VB6 ?
Using a recordset with several rows, you will need to loop through the rows to populate the listbox. And instead of a textbox, you ill need to use a Listbox I guess.
One way of doing it is as follows;
Dim iIndex as integer
If Not Recordset Is Nothing Then
For iIndex = 0 To Recordset.Fields.Count - 1
ListBox.Add(Recordset.Fields(iIndex).Column)
Next iIndex
End if
you can also use the While loop on the recordset
If Recordset.RecordCount > 0 Then
Recordset.MoveFirst
While Not Recordset.EOF
...
Recordset.MoveNext
Wend
End if