INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Visual Basic (Microsoft) Versions 5/6 FAQ

Access Version or Conversion

Create Acess Database, Tables and Fields through VB by s2001
Posted: 3 Jan 02

Hi all;

I am writing this FAQ because I have seen my question concerning creating Access Database, tables and field
and populating them. Use this sample code to work around.


option explicit
Private wrkDefault As Workspace
Private dbsNew As Database
Private tdfNew As New TableDef
Private cnn As New ADODB.Connection
Private cmd As New ADODB.Command
Private strcnn As String
Private rs As New ADODB.Recordset
Private sDataBaseName As String


Private Sub CreateDB()
On Error GoTo err

sDataBaseName = "c:\NewDB.mdb"
Set wrkDefault = DBEngine.Workspaces(0)
Set dbsNew = wrkDefault.CreateDatabase(sDataBaseName, _
    dbLangGeneral, dbEncrypt) 'Create New DataBase
Set dbsNew = wrkDefault.OpenDatabase(sDataBaseName, True, False) 'open the created Database
Set tdfNew = dbsNew.CreateTableDef("NewTable")  'Create Table

With tdfNew
  .fields.Append .CreateField("NewField", dbtext) 'Create a new field with text as data type
End With

dbsNew.TableDefs.Append tdfNew 'add the table to the DB
dbsNew.Close 'close database
err:
    If err.Number = 3204 Then 'DB exists
        Kill sDataBaseName
        Resume
    End If
End Sub

Private Sub populateDB()
cnn.ConnectionString = "ODBC;DBQ=" & sDataBaseName & ";UID=;PWD=;Driver={Microsoft Access Driver (*.mdb)}"
cnn.open
Set rs = New ADODB.Recordset
strcnn = "SELECT * FROM  NewTable"
rs.open strcnn, cnn, adOpenDynamic, adLockOptimistic
rs.addNew
rs.fields("NewField").Value = "Hello"
rs.Update
' To read from the DB into say a textbox named text1
' text1.text=rs.fields("NewField").Value

rs.Close
Set rs = Nothing
cnn.Close

MsgBox "Done. Database can be found at " & sDataBaseName, vbInformation + vbOKOnly

'******--------PS: Referance ADO 2.5 and DAO 3.6***********-


End Sub

Back to Visual Basic (Microsoft) Versions 5/6 FAQ Index
Back to Visual Basic (Microsoft) Versions 5/6 Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close