New coder, need help getting started - How do I set a ADO record set?

  • Thread starter news.east.cox.net
  • Start date
N

news.east.cox.net

I'm new to coding, took classes a few years back.

I would like to link an Access database to a form, using Access VB if
possible? I did this a few year back, but I'm having trouble now. Can any
one point me towards a good reference source.
How do I set up a ADO dataset to link to my database

I'm currently using Access2003.

I few years ago I completed a project (for class) in Access97 and or 2000.


Any help would be very grateful

Tony A.
 
A

Arvin Meyer [MVP]

By default, Access uses bound forms to connect to data. In the recordsource
property of the form, use the name of a table or query, or use an SQL
statement.

You can also build a recordset using DAO or ADO, and change the recordsource
of the form in code.
 
N

news.east.cox.net

Arvin, thank you for the reply.
I wanted to try to set up a recordset using ADO. Can I do this with just
Access, or do I need VB2008 or some other program?

I have a project I want to tackle, and this is my first step.

Thank you for your help.


Tony
 
A

Arvin Meyer [MVP]

You can do it in Access. Here's some sample code that I haven't tested yet:

Attribute VB_Name = "basFormBinding"
Option Compare Database
Option Explicit

Public Function SaveFormRecord(ByRef frm As Access.Form, ByVal
strStoredProcName As String, ByVal strRowGUID As String) As String

Dim rst As ADODB.Recordset
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim fld As ADODB.Field
Dim intParamDirection As Integer
Dim ctl As Access.Control
Dim strTag As String
On Error GoTo Proc_Err

'Get a reference to the form's underlying recordset
Set rst = frm.Recordset
'Go to the current record on the form
rst.Bookmark = frm.Bookmark

'Create a new command object as a stored proc call
Set cmd = New ADODB.Command
With cmd
.CommandType = adCmdStoredProc
.CommandText = strStoredProcName
End With
'Enumerate through form recordset fields and setup parameter definitions and
set value to the field value
For Each fld In rst.Fields
If fld.Name = strRowGUID Then
intParamDirection = adParamInputOutput
Else
intParamDirection = adParamInput
End If
With cmd
.Parameters.Append .CreateParameter("@" & fld.Name, fld.Type,
intParamDirection, fld.DefinedSize, fld.Value)
End With
Next fld

'Enumerate through the controls collection and set the parameter values (to
replace the original value set by the underlying recordset

For Each ctl In frm.Controls
strTag = ctl.Tag
If Len(strTag) > 0 Then
cmd.Parameters("@" & strTag).Value = ctl.Value
End If
Next ctl

'reference the project's current connection
Set cmd.ActiveConnection = CurrentProject.Connection
cmd.Execute , , adExecuteNoRecords
SaveFormRecord = cmd.Parameters("@" & strRowGUID).Value

Proc_Exit:
On Error Resume Next
Set rst = Nothing
Set fld = Nothing
Set cmd = Nothing
Set ctl = Nothing
Exit Function

Proc_Err:
Err.Raise Err.Number, "SaveFormRecord", Err.Description
Resume Proc_Exit
End Function

Public Sub PopulateForm(ByRef frm As Access.Form)
Dim ctl As Access.Control
Dim rst As ADODB.Recordset
Dim strTag As String
On Error GoTo Proc_Err

If Not frm.NewRecord Then
Set rst = frm.Recordset
rst.Bookmark = frm.Bookmark

For Each ctl In frm.Controls
strTag = ctl.Tag
If Len(strTag) > 0 Then
'Set value of control the the value of the field
ctl.Value = rst.Fields(strTag).Value
End If
Next
Else
For Each ctl In frm.Controls
strTag = ctl.Tag
If Len(strTag) > 0 Then
With ctl
'If there is a default value then evaluate it
If Len(.DefaultValue) > 0 Then
.Value = Eval(.DefaultValue)
Else
.Value = Null
End If
End With
End If
Next
End If

Proc_Exit:
On Error Resume Next
Set rst = Nothing
Set ctl = Nothing
Exit Sub

Proc_Err:
Err.Raise Err.Number, "PopulateForm", Err.Description
Resume Proc_Exit
End Sub

Public Sub UnbindForm(ByVal strForm As String)
Dim frm As Access.Form
Dim ctl As Access.Control
Dim fIsDataControl As Boolean
Dim strControlPrefix As String
Dim strControlSource As String
On Error GoTo Proc_Err

'Open form in design view
DoCmd.OpenForm strForm, acDesign


Set frm = Forms(strForm)

For Each ctl In frm.Controls
'Determine if the control is a data bound control and get naming prefix
Select Case ctl.ControlType
Case acTextBox
fIsDataControl = True
strControlPrefix = "txb"
Case acComboBox
fIsDataControl = True
strControlPrefix = "cbo"
Case acListBox
fIsDataControl = True
strControlPrefix = "lst"
Case acCheckBox
fIsDataControl = True
strControlPrefix = "chk"
Case acOptionButton
fIsDataControl = True
strControlPrefix = "opt"
Case acToggleButton
fIsDataControl = True
strControlPrefix = "tgl"
Case acOptionGroup
fIsDataControl = True
strControlPrefix = "fra"
Case Else
fIsDataControl = False
End Select
If fIsDataControl Then
'Get control source
strControlSource = ctl.ControlSource
If Len(strControlSource) > 0 Then
'If not a calculated control then unbind
If Left$(strControlSource, 1) <> "=" Then
With ctl
.Tag = strControlSource 'set control source to the tag
property
.ControlSource = "" 'Wipe the control source (unbind)
.Name = strControlPrefix & strControlSource 'rename the
control to convention
End With
End If
End If
End If
Next ctl

'Close and save changes
DoCmd.Close acForm, strForm, acSaveYes

Proc_Exit:
On Error Resume Next
Set ctl = Nothing
Set frm = Nothing
Exit Sub

Proc_Err:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error in
Procedure UnbindForm!"
Resume Proc_Exit
End Sub
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com
 
T

Tony

Thank you, I'll try it.




Arvin Meyer said:
You can do it in Access. Here's some sample code that I haven't tested
yet:

Attribute VB_Name = "basFormBinding"
Option Compare Database
Option Explicit

Public Function SaveFormRecord(ByRef frm As Access.Form, ByVal
strStoredProcName As String, ByVal strRowGUID As String) As String

Dim rst As ADODB.Recordset
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim fld As ADODB.Field
Dim intParamDirection As Integer
Dim ctl As Access.Control
Dim strTag As String
On Error GoTo Proc_Err

'Get a reference to the form's underlying recordset
Set rst = frm.Recordset
'Go to the current record on the form
rst.Bookmark = frm.Bookmark

'Create a new command object as a stored proc call
Set cmd = New ADODB.Command
With cmd
.CommandType = adCmdStoredProc
.CommandText = strStoredProcName
End With
'Enumerate through form recordset fields and setup parameter definitions
and set value to the field value
For Each fld In rst.Fields
If fld.Name = strRowGUID Then
intParamDirection = adParamInputOutput
Else
intParamDirection = adParamInput
End If
With cmd
.Parameters.Append .CreateParameter("@" & fld.Name, fld.Type,
intParamDirection, fld.DefinedSize, fld.Value)
End With
Next fld

'Enumerate through the controls collection and set the parameter values
(to replace the original value set by the underlying recordset

For Each ctl In frm.Controls
strTag = ctl.Tag
If Len(strTag) > 0 Then
cmd.Parameters("@" & strTag).Value = ctl.Value
End If
Next ctl

'reference the project's current connection
Set cmd.ActiveConnection = CurrentProject.Connection
cmd.Execute , , adExecuteNoRecords
SaveFormRecord = cmd.Parameters("@" & strRowGUID).Value

Proc_Exit:
On Error Resume Next
Set rst = Nothing
Set fld = Nothing
Set cmd = Nothing
Set ctl = Nothing
Exit Function

Proc_Err:
Err.Raise Err.Number, "SaveFormRecord", Err.Description
Resume Proc_Exit
End Function

Public Sub PopulateForm(ByRef frm As Access.Form)
Dim ctl As Access.Control
Dim rst As ADODB.Recordset
Dim strTag As String
On Error GoTo Proc_Err

If Not frm.NewRecord Then
Set rst = frm.Recordset
rst.Bookmark = frm.Bookmark

For Each ctl In frm.Controls
strTag = ctl.Tag
If Len(strTag) > 0 Then
'Set value of control the the value of the field
ctl.Value = rst.Fields(strTag).Value
End If
Next
Else
For Each ctl In frm.Controls
strTag = ctl.Tag
If Len(strTag) > 0 Then
With ctl
'If there is a default value then evaluate it
If Len(.DefaultValue) > 0 Then
.Value = Eval(.DefaultValue)
Else
.Value = Null
End If
End With
End If
Next
End If

Proc_Exit:
On Error Resume Next
Set rst = Nothing
Set ctl = Nothing
Exit Sub

Proc_Err:
Err.Raise Err.Number, "PopulateForm", Err.Description
Resume Proc_Exit
End Sub

Public Sub UnbindForm(ByVal strForm As String)
Dim frm As Access.Form
Dim ctl As Access.Control
Dim fIsDataControl As Boolean
Dim strControlPrefix As String
Dim strControlSource As String
On Error GoTo Proc_Err

'Open form in design view
DoCmd.OpenForm strForm, acDesign


Set frm = Forms(strForm)

For Each ctl In frm.Controls
'Determine if the control is a data bound control and get naming prefix
Select Case ctl.ControlType
Case acTextBox
fIsDataControl = True
strControlPrefix = "txb"
Case acComboBox
fIsDataControl = True
strControlPrefix = "cbo"
Case acListBox
fIsDataControl = True
strControlPrefix = "lst"
Case acCheckBox
fIsDataControl = True
strControlPrefix = "chk"
Case acOptionButton
fIsDataControl = True
strControlPrefix = "opt"
Case acToggleButton
fIsDataControl = True
strControlPrefix = "tgl"
Case acOptionGroup
fIsDataControl = True
strControlPrefix = "fra"
Case Else
fIsDataControl = False
End Select
If fIsDataControl Then
'Get control source
strControlSource = ctl.ControlSource
If Len(strControlSource) > 0 Then
'If not a calculated control then unbind
If Left$(strControlSource, 1) <> "=" Then
With ctl
.Tag = strControlSource 'set control source to the tag
property
.ControlSource = "" 'Wipe the control source (unbind)
.Name = strControlPrefix & strControlSource 'rename the
control to convention
End With
End If
End If
End If
Next ctl

'Close and save changes
DoCmd.Close acForm, strForm, acSaveYes

Proc_Exit:
On Error Resume Next
Set ctl = Nothing
Set frm = Nothing
Exit Sub

Proc_Err:
MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error in
Procedure UnbindForm!"
Resume Proc_Exit
End Sub
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com
 
T

Tony

Hi Arvin,
this code seems complex to me, but I will figure it out. It may take me
a while

Thank you for the challenge.

Tony
 
A

Armen Stein

this code seems complex to me, but I will figure it out. It may take me
a while

Thank you for the challenge.

Tony

Hi Tony,

If you're just getting started with Access development again, then why
not use bound forms as Arvin suggests? The ADO recordset method
you're trying is much more complex, and shouldn't be used unless
absolutely necessary. In our shop we use normal bound forms in almost
all of our development projects and they work fine.

Swimming downstream is much easier than swimming upstream!

Best regards,

Armen Stein
Microsoft Access MVP
www.JStreetTech.com
 
T

Tony

Armen,

I was looking toward the future; but you have a point. I can work on a
basic routine and then if needed change (or update) to ADO recordset if
required.
Thank you for the input.

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top