Insert Record

Ragnar78

Board Regular
Joined
Feb 10, 2004
Messages
210
Hy,

I'm totaly new in Access VBA, and i'm trying to create a macro that inserts a Record to a table i created

Code:
Sub insertRecord()

With Application.CurrentDb.OpenRecordset("Retreived Data")
                    .AddNew
                    !NUMBERPRGN = "C05555"
                    !TH_STATUS = "New"
                    !SYSMODTIME = Now
                    !DATE_ENTERED = Now
                    .Update
                    .Bookmark = .LastModified

End With

End Sub

Retreived Data is the name of my table.

This macro is not inserting a record, can anyone help me with it?
Thanks again...
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Easiiest way is to create a Macro, not using code!

Use the Macro to open the form or table you want the new record in.
Use the GoTo function next and set the crireria to New.

Easy as that.

I think you can also you the Goto Record function in code.
 
Upvote 0
Try this change:
I think the 'default' operation is Dynaset, but if not, this would explictly declare it.

Code:
With Application.CurrentDb.OpenRecordset("Retreived Data", dbOpenDynaset)

Another option is to change this from working with a table to working with a recordset. This is actually my own personal preference for how to do the code.

Code:
Sub insertRecord() 
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As Sring

Set dbs = CurrentDb()

strSQL = "SELECT * FROM [Retreived Data]"
Set rs = dbs.OpenRecordSet(strSQL, dbOpenDynaset)

With rs 
     .AddNew 
     !NUMBERPRGN = "C05555" 
     !TH_STATUS = "New" 
     !SYSMODTIME = Now 
     !DATE_ENTERED = Now 
     .Update 
     .Bookmark = .LastModified 
End With 

Set rs = Nothing
Set dbs = Nothing
End Sub

The idea behind this kind of format is it standardizes your approach. Also, by putting the SQL string into a variable, it simplifies those lines in your code making them easier to read. And lastly, if you don't need to open up an entire table for your task, don't. If you only need 3 out of 10 fields, you should get a performance improvement (code) by specifying only them.

As info, yes, you could substitute your tablename directly for the full SQL string.

Mike
 
Upvote 0

Forum statistics

Threads
1,221,691
Messages
6,161,309
Members
451,696
Latest member
Senthil Murugan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top