Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a new record to a table in an Access db 2

Status
Not open for further replies.

scrano

Technical User
Jan 24, 2001
51
AU
Hi can anyone tell me how to add a new record to a table in Access from within VB code and also how to check to see if a file exists already on a hard drive? Any help on this be greatly appreciated. Thanx in advance
 
u can check ur record using a query like
dim db as database
dim a as recordset
dim b as recordset
set b=db.openrecordset("emp")
set a=db.openRecordset("select * from emp where empno=urs new value")
if a.reccount>=1 then
msgbox "Record already exists"
else
b.addnew
b("name")="names"
'insert ur field values
end if

try this

with regards
webspy
 
Hi,

There is 2 ways of inserting row in tables. Webspy shows one of them. Note, however that not all providers support .recordcount, and that the cursortype should be 'static' to support .recordcount. You also need to update the recordset after .addnew:

---------------------------------------------------------
Dim Rst As ADODB.Recordset, con As ADODB.Connection
Set con = New ADODB.Connection
con.Open YourConnectionString
Set Rst = New ADODB.Recordset
Rst.Open "SELECT * FROM MyTable", con, adOpenKeyset, adLockOptimistic
Rst.AddNew Array("name", "title"), Array("sunaj", "Technical user")
Rst.Update
Rst.Close
Set Rst = Nothing
con.Close
Set con = Nothing
---------------------------------------------------------

The other method is to use SQL:
---------------------------------------------------------
Dim con As ADODB.Connection
Set con = New ADODB.Connection
con.Open YourConnectionString
con.execute "INSERT INTO Mytable(name,title) VALUES ('sunaj','Technical user')"
con.Close
Set con = Nothing
---------------------------------------------------------


Sunaj
 
Hi scrano

You are welcome. If you get some answer that you like - use the 'Let .... know this post was helpful!'

*:->*sunaj
 

I wrote that message just a little bit too fast...

Sunaj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top