Why the new table has to be created is a little hard to explain, but i tried to cut away the process and as it turns out, it does something vital to the process, so there is no getting around it.
That would be strange if you can not use a saved table instead of creating a new table every time..
So this code is executed from a button click. You could merge the code into some other process, if needed, to automate it.
(BTW, is the table name really "tblKRPdeelNemersTotaal"?)
Code:
Private Sub Command1_Click()
Dim dbs As DAO.Database
Dim tdfNew As TableDef
Dim strTableName As String
Dim strFieldName1 As String
Dim strFieldName2 As String
Dim resp As Boolean
strTableName = "tblKRPdeelNemersTotaal"
strFieldName1 = "MinVanvalidfrom"
strFieldName2 = "MaxVanvalidto"
Set dbs = CurrentDb
Set tdfNew = dbs.TableDefs(strTableName)
resp = SetAccessProperty(tdfNew.Fields(strFieldName1), "Format", 10, "dd-mm-yyyy hh:nn:ss")
' If resp Then
' MsgBox "Field 1 success"
' Else
' MsgBox "Field 1 failed"
' End If
resp = SetAccessProperty(tdfNew.Fields(strFieldName2), "Format", 10, "dd-mm-yyyy hh:nn:ss")
' If resp Then
' MsgBox "Field 2 success"
' Else
' MsgBox "Field 2 failed"
' End If
End Sub
Add this code to a standard module
Code:
Function SetAccessProperty(obj As Object, strName As String, _
intType As Integer, varSetting As Variant) As Boolean
Const conPropNotFound As Integer = 3270
Dim prp As Property
On Error GoTo ErrorSetAccessProperty
obj.Properties(strName) = varSetting
obj.Properties.Refresh
SetAccessProperty = True
ExitSetAccessProperty:
Exit Function
ErrorSetAccessProperty:
If Err = conPropNotFound Then
Set prp = obj.CreateProperty(strName, intType, varSetting)
obj.Properties.Append prp
obj.Properties.Refresh
SetAccessProperty = True
Resume ExitSetAccessProperty
Else
MsgBox Err & ": " & vbCrLf & Err.Description
SetAccessProperty = False
Resume ExitSetAccessProperty
End If
End Function