Results 1 to 2 of 2

Thread: Help needed in constructing create table statement

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow Help needed in constructing create table statement

    Well i am tring to construct create table statement for tables in my access db. I need an expert show me how i can
    force ShowFields module to return field and feild type value and place them in starBase line before i writing that statement to text file:

    strBase = "CREATE TABLE [" & Me![ComboBox] & "] ????

    The program workes like this. I created a command button and combo box inside a form. I select a table from combo box and press the button to generate the table statement for that table.I would be happy if some expert show me how to complete this task.Thanks

    code:

    Private Sub CreateTableButton_Click()

    Dim fso As New Scripting.FileSystemObject
    Dim io As Scripting.TextStream

    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim fld As DAO.Field
    '''Declreaing our variables
    Dim strBase As String

    Dim strFile As String
    Dim strName As String

    Set db = CurrentDb()

    Set rst = db.OpenRecordset(Me![ComboBox])



    '' This is a call for module that prints the feild name and type but i want some how it brings it here and put it in
    '' the next line of code ???????????
    Call ShowFields("Customers")

    '' IN the next line i want to put the field names and type before i insert it to a text file


    'construct sql statement and add to file text:????????????????????????????????????????????????
    strBase = "CREATE TABLE [" & Me![ComboBox] & "] ===============> CREATE Table Statement???
    strName = "c:\" & Me!ComboBox & " Data.sql"


    'write the statements to a file:
    If Len(strFile) > 0 Then
    Set io = fso.CreateTextFile(strName)
    io.Write strFile
    io.Close
    End If


    ExitHere:
    On Error Resume Next
    Set io = Nothing
    Set fso = Nothing
    Set rst = Nothing
    Set db = Nothing
    Exit Sub
    ErrHandler:
    Debug.Print Err, Err.Description
    Resume ExitHere

    End Sub

    ''This module displays field name and type in a massage box
    Public Sub ShowFields(pTable As String)

    Dim db As Database
    Dim rs As Recordset
    Dim i As Integer
    Dim j As Integer
    Dim n As Integer
    Dim NL As String
    Dim strHold As String
    Dim x As Variant

    NL = Chr(13) & Chr(10) ' Define newline.

    Set db = CurrentDb

    Set rs = db.OpenRecordset(pTable)

    n = rs.Fields.Count
    ReDim x(n - 1, 1) As String
    For i = 0 To (n - 1)
    x(i, 0) = rs.Fields(i).Name
    j = rs.Fields(i).Type
    x(i, 1) = Choose(j, "Boolean", "Byte", "Integer", "Long", "Currency", _
    "Single", "Double", "Date", "Binary", "Text")

    Next i
    rs.Close
    db.Close
    Set db = Nothing
    For i = 0 To (n - 1)
    'here the feild name and field type is generated
    strHold = strHold & x(i, 0) & "-" & x(i, 1) & IIf(i < (n - 1), NL, "")
    Next i
    MsgBox strHold, vbOKOnly, "Fields in table " & pTable

    End Sub

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Help needed in constructing create table statement

    Check out this thread in CodeBank on creating tables/fields.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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