Still can't work it out.
This is the full Sub that puts the Excel VBA array in an Access table:
Sub Array2Access()
Dim Db1 As DAO.Database
Dim tdfNew As TableDef
Dim Rs1 As DAO.Recordset
Dim idxPatient As Index
Dim strField As String
Dim n As Long
Dim i As Long
Dim c As Long
Dim strAccessFile As String
Dim newFieldArray
Dim strDateFormat2 As String
Dim strDate As String
strAccessFile = strLocalDrive & ":\RBSSynergyReporting\AddinTable.mdb"
'to get the real fieldcount and avoiding empty fields
'----------------------------------------------------
FieldCount = getLastRow1D(fieldArray)
'to leave the old fieldArray as it is
'------------------------------------
newFieldArray = fieldArray
'make sure we have valid names for the Access fields
'---------------------------------------------------
For i = 1 To FieldCount
newFieldArray(i) = MakeValidAccessFieldName(newFieldArray(i))
Next
'make sure the file is not there yet
If Len(Dir(strAccessFile)) > 0 Then
On Error GoTo ACCESS_OPEN
Kill strAccessFile
On Error GoTo 0
End If
'the Access date field won't for example accept ddd dd/mmm/yyyy
'--------------------------------------------------------------
If InStr(1, strDateFormat, " ", vbBinaryCompare) > 0 Or _
InStr(1, strDateFormat, ",", vbBinaryCompare) > 0 Or _
InStr(1, strDateFormat, ".", vbBinaryCompare) > 0 Or _
InStr(1, strDateFormat, "_", vbBinaryCompare) > 0 Or _
InStr(1, strDateFormat, "-", vbBinaryCompare) > 0 Or _
InStr(1, strDateFormat, "\", vbBinaryCompare) > 0 Or _
InStr(1, strDateFormat, "|", vbBinaryCompare) > 0 Or _
InStr(1, strDateFormat, "#", vbBinaryCompare) > 0 Then
'strDateFormat2 = "dd/mm/yyyy"
Else
'strDateFormat2 = strDateFormat
End If
'First, create the database.
Set Db1 = DBEngine.CreateDatabase(strAccessFile, dbLangGeneral)
'Create a new TableDef object.
Set tdfNew = Db1.CreateTableDef(strSheetName)
With tdfNew
' Create fields and append them to the new TableDef
' object. This must be done before appending the
' TableDef object to the TableDefs collection database.
.Fields.Append .CreateField("PATIENT_ID", dbLong)
.Fields("PATIENT_ID").Required = True
Set idxPatient = .CreateIndex("PatientIndex")
With idxPatient
.Fields.Append .CreateField("PATIENT_ID", dbLong)
.Primary = True
.Unique = True
End With
.Indexes.Append idxPatient
'remaining ID fields
For c = 2 To SeparatorArray(1, 1) - 1
If c > 255 Then
MsgBox "AN ACCESS TABLE CAN'T HAVE MORE THAN 255
FIELDS" & _
vbCrLf & vbCrLf & _
"CAN'T MAKE THIS TABLE", , _
"making Access table"
Db1.Close
Exit Sub
End If
strField = newFieldArray(c)
If InStr(1, strField, "age", vbTextCompare) > 0 Then
.Fields.Append .CreateField(strField, dbLong)
Else
If InStr(1, strField, "dob", vbTextCompare) > 0 Then
.Fields.Append .CreateField(strField, dbDate)
SetPropertyDAO .Fields(strField), "Format", dbText,
"dd/mmm/yyyy"
Else
.Fields.Append .CreateField(strField, dbText)
End If
End If
.Fields(strField).AllowZeroLength = True
Next
'non-ID fields or secondary ID fields
For c = SeparatorArray(1, 1) To FieldCount
If c > 255 Then
MsgBox "AN ACCESS TABLE CAN'T HAVE MORE THAN 255
FIELDS" & _
vbCrLf & vbCrLf & _
"CAN'T MAKE THIS TABLE", , _
"making Access table"
Db1.Close
Exit Sub
End If
strField = newFieldArray(c) & c
If InStr(1, strField, "systolic", vbTextCompare) > 0 Or _
InStr(1, strField, "diastolic", vbTextCompare) > 0 Then
.Fields.Append .CreateField(strField, dbLong)
Else
If InStr(1, strField, "date", vbTextCompare) > 0 Then
.Fields.Append .CreateField(strField, dbDate)
SetPropertyDAO .Fields(strField), "Format", dbText,
strDateFormat
Else
If InStr(1, strField, "value", vbTextCompare) > 0 Then
.Fields.Append .CreateField(strField, dbDouble)
Else
.Fields.Append .CreateField(strField, dbText)
End If
End If
End If
.Fields(strField).AllowZeroLength = True
Next
'Append the new TableDef object database.
Db1.TableDefs.Append tdfNew
End With
'Then, open the recordset.
Set Rs1 = Db1.OpenRecordset(strSheetName, dbOpenDynaset)
'Then write the data from the array to the recordset.
'Note that for each new record, you must first call Addnew
'then set the value property of the fields, and then call Update.
With Rs1
For i = 1 To URowCount
ShowProgressMessage strStatusIndent & _
"Making Access table, please wait ... "
.AddNew
.Fields("PATIENT_ID") = TableArray(i, 1)
'primary ID nodes
'----------------
For c = 2 To SeparatorArray(1, 1) - 1
strField = newFieldArray(c)
If InStr(1, fieldArray(c), "date", vbTextCompare) > 0 Or _
InStr(1, fieldArray(c), "dob", vbTextCompare) > 0 Then
On Error Resume Next 'to cover NULL values
'.Fields(strField) = Format(TableArray(i, c),
strDateFormat)
.Fields(strField) = TableArray(i, c)
On Error GoTo 0
Else
.Fields(strField) = TableArray(i, c)
End If
Next
'other nodes
'-----------
For c = SeparatorArray(1, 1) To FieldCount
strField = newFieldArray(c) & c
If InStr(1, fieldArray(c), "date", vbTextCompare) > 0 Then
On Error Resume Next 'to cover NULL values
'.Fields(strField) = Format(TableArray(i, c),
strDateFormat2)
.Fields(strField) = TableArray(i, c)
On Error GoTo 0
Else
.Fields(strField) = TableArray(i, c)
End If
Next
.Update
UpdateTimer 0, 0, 0, 0, False
MainForm.ProgressBar1.Value = ((i - 1) / URowCount) * 100
DoEvents
Next
End With
'Close the database.
Db1.Close
OpenAccessTable
Exit Sub
ACCESS_OPEN:
On Error GoTo 0
MsgBox "Can't delete the old file " & _
strAccessFile & " as it is open." & _
vbCrLf & vbCrLf & _
"Have to close it first.", _
vbExclamation, _
"making Access table"
End Sub
I keep getting dd/mm/yyyy
The reason is the function SetPropertyDAO gets an error Invalid operation at
the line:
obj.Properties.Append obj.CreateProperty(strPropertyName, intType, varValue)
The datevalues in TableArray(i, c) are Excel integer date numbers.
Must be overlooking something simple here and thanks for any advice.
RBS