entering data to table via vb code

  • Thread starter Thread starter stu dongel
  • Start date Start date
S

stu dongel

hello there

i am having trouble apending data to a table with vb code, i think my syntax
may be off with dao or something,
what i am trying to do is this, when you click cmd button it simply adds the
value of txtUserName to a table called tblUserTracking to the field
UserName. when i click the command button nothing happens, no error, no
appending of data to the table, does anyone se what wrong with this code?



Private Sub Command10_Click()


Dim wspJet As DAO.Workspace
Dim dbsCurrent As DAO.Database

Dim rstWrite As DAO.Recordset
Set wspJet = DBEngine.Workspaces(0)
Set MyDB = CurrentDb
Set rstWrite = MyDB.OpenRecordset("tblUserTracking", dbOpenDynaset,
dbAppendOnly)
wspJet.BeginTrans

rstWrite.AddNew
rstWrite!UserName = txtUserName.Value
rstWrite.Update

End Sub



thanks in advance
stu
 
You haven't declared the MyDB variable (although, if you don't use Option
Explicit, that shouldn't matter)...

Maybe you could try;

Dim myRecords As DAO.Recordset
Set myRecords = CurrentDB().OpenRecordset("SELECT * FROM
[tblUserTracking];")
myRecords.AddNew
myRecords![UserName] = txtUserName.Value
myRecords.Update
myRecords.Close
Set myRecords = Nothing


Also check, to see if there are other fields in your table, specifically
text or string fields, that do not allow zero length or have the required
property set. If you don't fill these, then the record wont be added
either...
 
thanks ruskin, it worked, much appriciations
stu
Ruskin said:
You haven't declared the MyDB variable (although, if you don't use Option
Explicit, that shouldn't matter)...

Maybe you could try;

Dim myRecords As DAO.Recordset
Set myRecords = CurrentDB().OpenRecordset("SELECT * FROM
[tblUserTracking];")
myRecords.AddNew
myRecords![UserName] = txtUserName.Value
myRecords.Update
myRecords.Close
Set myRecords = Nothing


Also check, to see if there are other fields in your table, specifically
text or string fields, that do not allow zero length or have the required
property set. If you don't fill these, then the record wont be added
either...



stu dongel said:
hello there

i am having trouble apending data to a table with vb code, i think my syntax
may be off with dao or something,
what i am trying to do is this, when you click cmd button it simply adds the
value of txtUserName to a table called tblUserTracking to the field
UserName. when i click the command button nothing happens, no error, no
appending of data to the table, does anyone se what wrong with this code?



Private Sub Command10_Click()


Dim wspJet As DAO.Workspace
Dim dbsCurrent As DAO.Database

Dim rstWrite As DAO.Recordset
Set wspJet = DBEngine.Workspaces(0)
Set MyDB = CurrentDb
Set rstWrite = MyDB.OpenRecordset("tblUserTracking", dbOpenDynaset,
dbAppendOnly)
wspJet.BeginTrans

rstWrite.AddNew
rstWrite!UserName = txtUserName.Value
rstWrite.Update

End Sub



thanks in advance
stu
 
Back
Top