CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 1999
    Location
    Québec (Canada)
    Posts
    210

    How can I create the table (dbf) in my VB code

    I would like to create a table (dbf) in my code. Am able to create database (mdb) an put new table in this database but when is the time to make dbf table am not able. I will not create database but juste a table dbf.

    Thanks and sorry for my english
    Redg


  2. #2
    Join Date
    Mar 2001
    Posts
    20

    Re: How can I create the table (dbf) in my VB code

    I’d like to do the same thing, but for now am just sticking to writing to already made tables. If you figure out how to create DBF files, please let me know. Thanks.

    Mike


  3. #3
    Join Date
    Mar 2000
    Posts
    145

    Re: How can I create the table (dbf) in my VB code

    Since DBF tables are each stored in separate files, you don't actually create a database, as such, like you do with MDB files. Think of the 'database' as the actual directory you will be storing the tables (DBF files) into. Here is some code that creates a FoxPro 2.5 DBF file as well as a corresponding compound index (CDX) file. To run this code, you'll need to go to Project | References and add "Microsoft DAO 3.51".


    option Explicit
    Dim dbsMyDatabase as Database
    Dim rstMyRecordset as Recordset
    Dim idxMyIndex as Index

    private Sub Command1_Click()
    OpenDBF
    CreateTable
    CreateIndex
    OpenTable
    CloseTable
    CloseDBF
    End Sub

    Sub OpenDBF()
    Dim strDbsName as string

    'the application directory will be considered the 'database'
    strDbsName = App.Path
    'open the 'database'
    set dbsMyDatabase = Workspaces(0).OpenDatabase(strDbsName, false, false, "FoxPro 2.5;")
    End Sub

    Sub CreateTable()
    Dim MyTableDef as TableDef
    Dim myField as Field

    'create a table named TEST1.DBF
    set MyTableDef = dbsMyDatabase.CreateTableDef("Test1")
    'create the following table
    'Structure for database: TEST1.DBF
    'Number of data records: 0
    'date of last update : 08/26/00
    'Field Field Name Type Width Dec Index
    ' 1 TEXT Character 6
    ' 2 date date 8
    ' 3 DOUBLE Numeric 20 5
    ' 4 BOOL Logical 1
    ' 5 INTEGER Numeric 6
    ' 6 LONG Numeric 11
    ' 7 SINGLE Numeric 20 5
    '** Total ** 73
    'add fields to the table definition
    set myField = MyTableDef.CreateField("Text", dbText, 6)
    MyTableDef.Fields.Append myField
    set myField = MyTableDef.CreateField("date", dbDate, 8)
    MyTableDef.Fields.Append myField
    set myField = MyTableDef.CreateField("Double", dbDouble, 8)
    MyTableDef.Fields.Append myField
    set myField = MyTableDef.CreateField("Bool", dbBoolean, 1)
    MyTableDef.Fields.Append myField
    set myField = MyTableDef.CreateField("Integer", dbInteger, 8)
    MyTableDef.Fields.Append myField
    set myField = MyTableDef.CreateField("Long", dbLong, 8)
    MyTableDef.Fields.Append myField
    set myField = MyTableDef.CreateField("Single", dbSingle, 8)
    MyTableDef.Fields.Append myField
    'add the table to the database
    dbsMyDatabase.TableDefs.Append MyTableDef
    End Sub

    Sub CreateIndex()
    Dim tdfMyTableDef as TableDef
    Dim idxNewIndex as Index

    'get a pointer to TEST1.DBF
    set tdfMyTableDef = dbsMyDatabase!Test1
    'the index file will be named TEXT1.CDX
    'there will be an index tag called PRIMARY
    set idxNewIndex = tdfMyTableDef.CreateIndex("PRIMARY")
    'setup the fields that will be indexed
    idxNewIndex.Fields.Append idxNewIndex.CreateField("Text")
    idxNewIndex.Fields.Append idxNewIndex.CreateField("date")
    'add the fields to the index definition
    tdfMyTableDef.Indexes.Append idxNewIndex
    'create the index file
    tdfMyTableDef.Indexes.Refresh
    End Sub

    Sub OpenTable()
    'open TEST1.DBF
    ' FoxPro equivalent: USE TEST1
    set rstMyRecordset = dbsMyDatabase.OpenRecordset("TEST1")
    'activate the PRIMARY index
    'which is stored in the file TEST1.CDX
    ' FoxPro equivalent: set ORDER to PRIMARY
    rstMyRecordset.Index = "PRIMARY"
    End Sub

    Sub CloseTable()
    rstMyRecordset.Close
    set rstMyRecordset = nothing
    End Sub

    Sub CloseDBF()
    dbsMyDatabase.Close
    set dbsMyDatabase = nothing
    End Sub





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured