Hoi allemaal,
Op dit moment ben ik bezig met een project dat helemaal over MS Access gaat. De database die we nu hebben is aardig groot, zo'n 40 tabellen.
Om de tabellen uiteindelijk automatisch aan te kunnen maken gebruik ik een script in vba, om de create tables automatisch aan te maken.
Op een andere dbase werkt het script zonder enkele problemen, maar op de database die ik nu gebruik krijg ik een foutmelding te zien:
"Compileerfout: Een door de gebruiker gedefinieerd gegevenstype is niet gedefinieerd"
Hier is het script:
###########################
Sub exportSQL()
Dim db As Database
Set db = CurrentDb()
Open "c:\definitiequeries.txt" For Output As #1
Open "c:\validatieregels.txt" For Output As #2
Dim fieldList As String, fieldList2 As String
' loop through tables
Dim tableIndex As Integer
For tableIndex = 0 To db.TableDefs.Count - 1
Dim table As TableDef
Set table = db.TableDefs(tableIndex)
' only visible tables
If (((table.Attributes And DB_SYSTEMOBJECT) Or _
(table.Attributes And DB_HIDDENOBJECT))) = 0 Then
Print #1,: Print #1, "CREATE TABLE " & table.Name & " ("
Dim tableValidations As Boolean
If table.validationRule <> "" Then
Print #2,: Print #2, "Table " & table.Name & ": " & table.validationRule
tableValidations = True
Else
tableValidations = False
End If
fieldList = ""
' list datatypes
Dim fieldIndex As Integer
For fieldIndex = 0 To table.Fields.Count - 1
Dim field As field
Set field = table.Fields(fieldIndex)
' if this is not the first iteration, add separators
If fieldList <> "" Then
fieldList = fieldList & ", "
Print #1, ","
End If
fieldList = fieldList & field.Name
Dim typestr As String
If ((field.Attributes And dbAutoIncrField) <> 0) Then
typestr = "AUTOINCREMENT NOT NULL"
Else
Select Case field.Type
Case dbBinary: typestr = "BINARY"
Case dbBoolean: typestr = "YESNO"
Case dbCurrency: typestr = "CURRENCY"
Case dbDate: typestr = "DATETIME"
Case dbDecimal: typestr = "DECIMAL(20,4)"
Case dbFloat: typestr = "FLOAT"
Case dbSingle: typestr = "SINGLE"
Case dbDouble: typestr = "DOUBLE"
Case dbNumeric: typestr = "NUMERIC"
Case dbByte: typestr = "BYTE"
Case dbInteger: typestr = "INTEGER"
Case dbLong: typestr = "LONG"
Case dbMemo: typestr = "MEMO"
Case dbGUID: typestr = "GUID"
Case dbText:
If field.Size = 0 Then field.Size = 255
typestr = "CHAR(" & field.Size & ")"
Case Else
MsgBox "Table " & table.Name & " field " & field.Name & ": unrecognized type identifier"
End Select
If field.Required = True Then typestr = typestr & " NOT NULL"
End If
Print #1, Space$(INDENT_SIZE) & field.Name & " " & typestr;
Next fieldIndex
' list primary keys
Dim index As index
For Each index In table.Indexes
If index.Primary Then
fieldList = ""
Dim f As field
For Each f In index.Fields
If fieldList <> "" Then fieldList = fieldList & ", "
fieldList = fieldList & f.Name
Next f
Print #1, ",": Print #1, Space$(INDENT_SIZE) & "PRIMARY KEY (" & fieldList & ")";
End If
Next index
' list foreign keys
Dim r As Relation
For Each r In db.Relations
If r.ForeignTable = table.Name Then
Dim f3 As field
fieldList = ""
fieldList2 = ""
For Each f3 In r.Fields
If fieldList <> "" Then fieldList = fieldList & ", "
fieldList = fieldList & f3.Name
If fieldList2 <> "" Then fieldList2 = fieldList2 & ", "
fieldList2 = fieldList2 & f3.ForeignName
Next f3
Print #1, ",": Print #1, Space$(INDENT_SIZE) & "FOREIGN KEY (" & fieldList2 & ") REFERENCES " & r.table & "(" & fieldList & ")";
End If
Next r
' end query
Print #1,: Print #1, Space$(INDENT_SIZE) & ");"
' list field validation rules
For fieldIndex = 0 To table.Fields.Count - 1
Set field = table.Fields(fieldIndex)
If field.validationRule <> "" Then
If Not tableValidations Then
Print #2,: Print #2, "Table " & table.Name
tableValidations = True
End If
Print #2, field.Name & ": " & field.validationRule
End If
Next
End If
Next tableIndex
Close #1
Close #2
db.Close
End Sub
###########################
Na lang kijken kom ik tot de conclusie dat alle gegevenstypen die in deze dbase staan, ook allemaal gebruikt worden in die vorige database.
De help van access heeft het voornamelijk over een fout gegevenstype maar dat sluit ik als fout al uit.
Het runnen van het script hierboven heb ik op beide dbases gedaan, op mijn pc met dezelfde access 2000 versie.
Beide dbases zijn gemaakt in een nederlandse versie van access.
De regel: "Case dbText:"
Heb ik al een keer aangepast naar:
"Case dbTekst:"
of
"Case dbText: typestr = "TEXT"
of
"Case dbTekstt: typestr = "TEKST"
Maar dat heeft geen effect.
Heeft iemand een idee waar het fout zit? Of heeft iemand tips? Zoja, dan hoor ik dat graag
Op dit moment ben ik bezig met een project dat helemaal over MS Access gaat. De database die we nu hebben is aardig groot, zo'n 40 tabellen.
Om de tabellen uiteindelijk automatisch aan te kunnen maken gebruik ik een script in vba, om de create tables automatisch aan te maken.
Op een andere dbase werkt het script zonder enkele problemen, maar op de database die ik nu gebruik krijg ik een foutmelding te zien:
"Compileerfout: Een door de gebruiker gedefinieerd gegevenstype is niet gedefinieerd"
Hier is het script:
###########################
Sub exportSQL()
Dim db As Database
Set db = CurrentDb()
Open "c:\definitiequeries.txt" For Output As #1
Open "c:\validatieregels.txt" For Output As #2
Dim fieldList As String, fieldList2 As String
' loop through tables
Dim tableIndex As Integer
For tableIndex = 0 To db.TableDefs.Count - 1
Dim table As TableDef
Set table = db.TableDefs(tableIndex)
' only visible tables
If (((table.Attributes And DB_SYSTEMOBJECT) Or _
(table.Attributes And DB_HIDDENOBJECT))) = 0 Then
Print #1,: Print #1, "CREATE TABLE " & table.Name & " ("
Dim tableValidations As Boolean
If table.validationRule <> "" Then
Print #2,: Print #2, "Table " & table.Name & ": " & table.validationRule
tableValidations = True
Else
tableValidations = False
End If
fieldList = ""
' list datatypes
Dim fieldIndex As Integer
For fieldIndex = 0 To table.Fields.Count - 1
Dim field As field
Set field = table.Fields(fieldIndex)
' if this is not the first iteration, add separators
If fieldList <> "" Then
fieldList = fieldList & ", "
Print #1, ","
End If
fieldList = fieldList & field.Name
Dim typestr As String
If ((field.Attributes And dbAutoIncrField) <> 0) Then
typestr = "AUTOINCREMENT NOT NULL"
Else
Select Case field.Type
Case dbBinary: typestr = "BINARY"
Case dbBoolean: typestr = "YESNO"
Case dbCurrency: typestr = "CURRENCY"
Case dbDate: typestr = "DATETIME"
Case dbDecimal: typestr = "DECIMAL(20,4)"
Case dbFloat: typestr = "FLOAT"
Case dbSingle: typestr = "SINGLE"
Case dbDouble: typestr = "DOUBLE"
Case dbNumeric: typestr = "NUMERIC"
Case dbByte: typestr = "BYTE"
Case dbInteger: typestr = "INTEGER"
Case dbLong: typestr = "LONG"
Case dbMemo: typestr = "MEMO"
Case dbGUID: typestr = "GUID"
Case dbText:
If field.Size = 0 Then field.Size = 255
typestr = "CHAR(" & field.Size & ")"
Case Else
MsgBox "Table " & table.Name & " field " & field.Name & ": unrecognized type identifier"
End Select
If field.Required = True Then typestr = typestr & " NOT NULL"
End If
Print #1, Space$(INDENT_SIZE) & field.Name & " " & typestr;
Next fieldIndex
' list primary keys
Dim index As index
For Each index In table.Indexes
If index.Primary Then
fieldList = ""
Dim f As field
For Each f In index.Fields
If fieldList <> "" Then fieldList = fieldList & ", "
fieldList = fieldList & f.Name
Next f
Print #1, ",": Print #1, Space$(INDENT_SIZE) & "PRIMARY KEY (" & fieldList & ")";
End If
Next index
' list foreign keys
Dim r As Relation
For Each r In db.Relations
If r.ForeignTable = table.Name Then
Dim f3 As field
fieldList = ""
fieldList2 = ""
For Each f3 In r.Fields
If fieldList <> "" Then fieldList = fieldList & ", "
fieldList = fieldList & f3.Name
If fieldList2 <> "" Then fieldList2 = fieldList2 & ", "
fieldList2 = fieldList2 & f3.ForeignName
Next f3
Print #1, ",": Print #1, Space$(INDENT_SIZE) & "FOREIGN KEY (" & fieldList2 & ") REFERENCES " & r.table & "(" & fieldList & ")";
End If
Next r
' end query
Print #1,: Print #1, Space$(INDENT_SIZE) & ");"
' list field validation rules
For fieldIndex = 0 To table.Fields.Count - 1
Set field = table.Fields(fieldIndex)
If field.validationRule <> "" Then
If Not tableValidations Then
Print #2,: Print #2, "Table " & table.Name
tableValidations = True
End If
Print #2, field.Name & ": " & field.validationRule
End If
Next
End If
Next tableIndex
Close #1
Close #2
db.Close
End Sub
###########################
Na lang kijken kom ik tot de conclusie dat alle gegevenstypen die in deze dbase staan, ook allemaal gebruikt worden in die vorige database.
De help van access heeft het voornamelijk over een fout gegevenstype maar dat sluit ik als fout al uit.
Het runnen van het script hierboven heb ik op beide dbases gedaan, op mijn pc met dezelfde access 2000 versie.
Beide dbases zijn gemaakt in een nederlandse versie van access.
De regel: "Case dbText:"
Heb ik al een keer aangepast naar:
"Case dbTekst:"
of
"Case dbText: typestr = "TEXT"
of
"Case dbTekstt: typestr = "TEKST"
Maar dat heeft geen effect.
Heeft iemand een idee waar het fout zit? Of heeft iemand tips? Zoja, dan hoor ik dat graag
cd /usr/ports/www/porn make install