move the .Update outside the for each control loop.
Code:
For Each c In Me.Controls
rsvalues.AddNew
If c.Name = "txt" & rsvalues.Fields(i).Name Then
Select Case rsvalues.Fields(i).Type
Case adodb.DataTypeEnum.adVarChar, adodb.DataTypeEnum.adChar
rsvalues.Fields(i).Value = c.Text
Case adodb.DataTypeEnum.adDate
rsvalues.Fields(i).Value = CDate(c.Text)
Case adodb.DataTypeEnum.adInteger
rsvalues.Fields(i).Value = CInt(c.Text)
End Select
End If
Exit For
Next c
rsvalues.Update
That way it only does the update AFTer all fields have been updated.
And yeah, DataDel doesn't do anything. Two ways to delete. 1) Find the record in the recorset, then do a rs.Delete on it. or 2) create a DELETE SQL command and execute that.
yeah, if c.Text isn't a date compatible format or value, converting it to Date will fail. Probably should do a IsDate() check on it first, then set it if it's valid else set the field to NULL.
How many threads have you created for the same question?? This is confusing and annoying - I have to read 3 threads to find out what the current situation is!
Last problem standing is that it will not append the data to the fields
What exactly do you mean by this?
If you mean that it is adding a new record when you want to update the current one, just remove/comment out the "rsvalues.AddNew" line.
The only problem that remains is making the program bind the data to a field in the database. It will create new records, but it doesnt save the old ones.
VB Code:
Private Sub datanew_click()
'Add a new record
FillData_Click
rsvalues.AddNew
'Loop through each field
For i = 0 To rsvalues.Fields.Count - 1
'Loop through each control
For Each c In Me.Controls
'If the control name matched the field name
If c.Name = "txt" & rsvalues.Fields(i).Name Then
'Set it based on the data type
Select Case rsvalues.Fields(i).Type
Case adodb.DataTypeEnum.adVarChar, adodb.DataTypeEnum.adChar
rsvalues.Fields(i).Value = c.Text
If IsDate(c.Text) Then
rsvalues.Fields(i).Value = CDate(c.Text)
Else
rsvalues.Fields(i).Value = Null 'Or what ever default value you want
End If
Case adodb.DataTypeEnum.adInteger
rsvalues.Fields(i).Value = CInt(c.Text)
End Select
End If 'end if field matches control
Next c 'Get the next control
Next i 'Get the next field
'Update the recordset
rsvalues.Update
'Loop through the controls and blank each one out
For Each c In Me.Controls
If TypeOf c Is TextBox Then
c.Text = vbNullString
End If
Next
'Move to the next record
rsvalues.MoveNext
End Sub
Private Sub FillData_Click()
For i = 0 To rsvalues.Fields.Count - 1
'Loop through each control
For Each c In Me.Controls
'If the control name matched the field name
If c.Name = "txt" & rsvalues.Fields(i).Name Then
'Set it based on the data type
Select Case rsvalues.Fields(i).Type
Case adodb.DataTypeEnum.adVarChar, adodb.DataTypeEnum.adChar
rsvalues.Fields(i).Value = c.Text
If IsDate(c.Text) Then
rsvalues.Fields(i).Value = CDate(c.Text)
Else
rsvalues.Fields(i).Value = Null 'Or what ever default value you want
End If
Case adodb.DataTypeEnum.adInteger
rsvalues.Fields(i).Value = CInt(c.Text)
End Select
End If 'end if field matches control
Next c 'Get the next control
Next i 'Get the next field
'Update the recordset
rsvalues.Update
End Sub
See anything wrong here? It should be updating perfectly
It looks to me as if the FillData_click sub should save the values in the textboxes to the current record. ADO has no method to initiate Editing (DAO had rs.edit, but ADO assumes as soon as you set a value) so the code you have should work.
What is it actually doing? Is it being saved anywhere in the database table?
It looks to me as if the FillData_click sub should save the values in the textboxes to the current record. ADO has no method to initiate Editing (DAO had rs.edit, but ADO assumes as soon as you set a value) so the code you have should work.
What is it actually doing? Is it being saved anywhere in the database table?
No, all it does it create a new blank record and discards anything i type.
After deciphering your dodgy naming (of controls & subs) and strange indenting, here's a few issues for you:
In "DataConnect_Click": rather than having a new loop to fill the values (which shouldn't have a .Movefirst !), just Call the approriate sub instead (fillText_Click).
Behind the button labeled * , your code is:
VB Code:
Label13.Caption = "Added New Record"
datadel_click
..do you see a problem here? the same for the button *.* :
VB Code:
Label13.Caption = "Deleted Record"
datanew_click
At the end of "FillData_Click", you seem to have removed the "rs.values.update", which is the part that actually writes to the DB
"datanew_click": you call FillData_Click, which saves the textbox values to the current record, then you immediately add a new record and save those same values to it, and then clear the boxes. What you should do instead is:
start with "FillData_Click" if you want.
initiate the Addnew
clear the textboxes
have another event (possibly a new button) which calls FillData_Click.
I would personally have a button for saving, which will cover both of these options - note that as things currently stand you do not do anything to save when you move to a different record (you could call FillData_Click before any move commands).