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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making New Database , Exporting Data using VBA

Status
Not open for further replies.

MikeUK

Programmer
Sep 7, 2001
5
GB
Using Access ,I need a command button to do he following:
* Make a Blank database.
* Export all data in a specific query from the current database to the new database.

Does anyone know the VBA code for first creating a new Access file and also exporting data in a query to that file?
 
with dao

Sub sdaf()
Dim dbsNew As Database, tblDef As TableDef, rst, rstNew As Recordset, i, j As Long
Set dbsNew = CreateDatabase("c:\NewDB.mdb", _
dbLangGeneral, dbEncrypt)
Set rst = CurrentDb.OpenRecordset("SELECT * FROM SzelvMeretek")
Set tblDef = dbsNew.CreateTableDef("NewTable")
With tblDef
For i = 0 To rst.Fields.Count - 1
.Fields.Append .CreateField(rst.Fields(i).Name, rst.Fields(i).Type)
Next i
End With
dbsNew.TableDefs.Append tblDef
dbsNew.TableDefs.Refresh
Set rstNew = dbsNew.OpenRecordset("SELECT * from NewTable")
If rst.RecordCount > 0 Then
rst.MoveLast
rst.MoveFirst
For i = 1 To rst.RecordCount
With rstNew
.AddNew
For j = 0 To rst.Fields.Count - 1
.Fields(j) = rst.Fields(j)
Next j
.Update
End With
rst.MoveNext
Next i
End If
End Sub

ide
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top