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

Creating Databases? 1

Status
Not open for further replies.

capone67

Programmer
Nov 6, 2000
115
CA
Hi Gang

Does anyone know if it is possible to create a database using vb code? My other alternative is to figure out how to copy the database that I have included in my activex cab file to a specific location on the system running it.

Any input would be of great help.

Ken
 
you can by selecting references that is library any of these like activex data objects 2.0 library for selecting references go to project and then go to references
then declare a variable
Dim con As New ADODB.Connection
con.Open "provider=microsoft.jet.oledb.3.51;datasource=c:\riaz.mdb"
these portion is for connection

 
Ken
This is an example of Sams TYS Database programming with vb5. Hope this helps.
Niranjan


Option Explicit

Dim ws As Workspace
Dim db As Database
Dim td As TableDef
Dim fl As Field


Private Sub Command1_Click()
Dim strDBName As String
strDBName = "c:\MyDataBase.mdb"
On Error Resume Next

Set ws = DBEngine.Workspaces(0)
Set db = ws.CreateDatabase(strDBName, dbLangGeneral)
Set td = db.CreateTableDef("FieldTypes")

MakeField "BinaryField", dbBinary
MakeField "BooleanField", dbBoolean
MakeField "ByteField", dbByte
MakeField "CurrencyField", dbCurrency
MakeField "DateTimeField", dbDate
MakeField "DoubleField", dbDouble
MakeField "GUIDField", dbGUID
MakeField "IntegerField", dbInteger
MakeField "LongField", dbLong
MakeField "LongBinaryField", dbLongBinary
MakeField "MemoField", dbMemo
MakeField "SingleField", dbSingle
MakeField "TextField", dbText, 255

' invalid when creating JET databases
'MakeField "BigIntegerField", dbBigInt
'MakeField "CharField", dbChar, 5
'MakeField "DecimalField", dbDecimal
'MakeField "FloatField", dbFloat
'MakeField "NumericField", dbNumeric
'MakeField "TimeField", dbTime
'MakeField "TimeStampField", dbTimeStamp
'MakeField "VarBinaryField", dbVarBinary
'
' some special attributes
MakeField "AutoIncrField", dbLong, lngAttributes:=dbAutoIncrField
MakeField "HyperLinkField", dbMemo, lngAttributes:=dbHyperlinkField
'
db.TableDefs.Append td
'
MsgBox "Fields Added"
'
End Sub

Public Sub MakeField(strFieldName As String, intFieldType As Integer, Optional intSize As Integer, Optional lngAttributes As Long)
'
Set fl = td.CreateField(strFieldName, intFieldType)
'
If IsMissing(intSize) = False Then
fl.Size = intSize
End If
'
If IsMissing(lngAttributes) = False Then
fl.Attributes = lngAttributes
End If
'
td.Fields.Append fl
'
End Sub

Private Sub Command2_Click()
'
Dim strMsg As String
'
For Each fl In td.Fields
strMsg = ""
strMsg = strMsg & "Name: " & fl.Name & vbCrLf
strMsg = strMsg & "Type: " & CStr(fl.Type) & vbCrLf
strMsg = strMsg & "Size: " & CStr(fl.Size) & vbCrLf
strMsg = strMsg & "Attrib: " & CStr(fl.Attributes) & vbCrLf
MsgBox strMsg
Next
'
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top