Send data to Access database

R

red6000

Hi, I have the code below to send data based on whether optionbutton and
checkboxes were ticked or not to an access database.

Now I can get the code to work fine, but what I wanted to do was have a for
loop that tested each checkbox or optionbutton and then send the data
instead of having to write the code for every checkbox and optionbutton. To
do this, I am holding the fieldname from access in the tag property of the
optionbutton and checkbox.

For some reason, when the code gets to
vRecordSet!xArray(xCount).Tag = "Selected" it fails as it says it
can't find the field in the database. I have put a msgbox in front of this
line to test that it is pulling the tag property correctly and it is.

So what I am expecting in laymens terms is... If the tag proprty holds the
value 'csrName', then the code would become:
vRecordSet!csrName = "Selected"

As I say, if I actually code it with vRecordSet!csrName = "Selected"
it works.

Hope that makes sense.

Any ideas?

Thanks.

Dim xArray()
Dim xCount As Integer
Dim xControl As Control
Dim vConnection As New ADODB.Connection
Dim vRecordSet As New ADODB.Recordset

vConnection.connectionstring = "C:\mi.mdb" &
"Provider=Microsoft.Jet.OLEDB.4.0;"
vConnection.Open

vRecordSet.Open "miTable", vConnection, adOpenKeyset, adLockOptimistic
vRecordSet.AddNew

For Each xControl In myForm.Controls
If TypeOf xControl Is MSForms.CheckBox Or TypeOf xControl Is
MSForms.OptionButton Then
ReDim Preserve xArray(xCount)
Set xArray(xCount) = xControl
xCount = xCount + 1
End If
Next xControl

For xCount = 0 To UBound(xArray)
If xArray(xCount) = True Then
vRecordSet!xArray(xCount).Tag = "Selected"
Else
vRecordSet!xArray(xCount).Tag = "."
End If
Next xCount
 
R

red6000

fixed it by changing:
vRecordSet!xArray(xCount).Tag = "Selected"
Else
vRecordSet!xArray(xCount).Tag = "."
to

vRecordSet.Fields(xArray(xCount).Tag) = "Selected"
Else
vRecordSet.Fields(xArray(xCount).Tag) = "."
 

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