Dear all,
If somebody could help me, then I'd be so grateful! All due to sheer inexperience of vba programming.
I would like all of the following to occur at the click of a button on a form, hence my private sub command0_Click() but when I started to put together my 'If' logic, all started to fail. How can I correct this so that not only does it create the 3 fields for me, but it also populates other fields with particular data using the specified logic?
Here is my code so far:
Private Sub Command0_Click()
'Need to create 3 new fields: CSP_Level, Import_Date and Count
On Error GoTo ErrHandler
Dim db As Database
Dim tbl As TableDef
Dim fld As DAO.Field
Set db = CurrentDb()
Set tbl = db.TableDefs("CSP Export")
Set fld = tbl.CreateField("CSP_Level", dbText, 30)
tbl.Fields.Append fld
'set ordinal position
fld.OrdinalPosition = 1
fld.Properties("Required").Value = True
fld.Properties("AllowZeroLength").Value = False
Set fld = tbl.CreateField("Import_Date", dbDate)
tbl.Fields.Append fld
fld.Properties("Required").Value = True
fld.Properties("AllowZeroLength").Value = False
Set fld = tbl.CreateField("Count", dbInteger)
tbl.Fields.Append fld
fld.Properties("Required").Value = True
fld.Properties("AllowZeroLength").Value = False
'Populate the CSP_Level (Rec_Ref, Location_Name and Event_Title are fields within the table
Function CSP_Level(Rec_Ref, Location_Name, Event_Title)
If Rec_Ref > "" Then CSP_Level = "Project"
ElseIf Location_Name > "" And Event_Title Is Null Then CSP_Level = "Site"
ElseIf Event_Title > "" Then CSP_Level = "Event"
End If
CleanUp:
Set fld = Nothing
Set tbl = Nothing
Set db = Nothing
Exit Function
ErrHandler:
MsgBox "Error in addFieldToTbl( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp
End Function
End Sub
Thank you for any help that you can give,
sparkes84
If somebody could help me, then I'd be so grateful! All due to sheer inexperience of vba programming.
I would like all of the following to occur at the click of a button on a form, hence my private sub command0_Click() but when I started to put together my 'If' logic, all started to fail. How can I correct this so that not only does it create the 3 fields for me, but it also populates other fields with particular data using the specified logic?
Here is my code so far:
Private Sub Command0_Click()
'Need to create 3 new fields: CSP_Level, Import_Date and Count
On Error GoTo ErrHandler
Dim db As Database
Dim tbl As TableDef
Dim fld As DAO.Field
Set db = CurrentDb()
Set tbl = db.TableDefs("CSP Export")
Set fld = tbl.CreateField("CSP_Level", dbText, 30)
tbl.Fields.Append fld
'set ordinal position
fld.OrdinalPosition = 1
fld.Properties("Required").Value = True
fld.Properties("AllowZeroLength").Value = False
Set fld = tbl.CreateField("Import_Date", dbDate)
tbl.Fields.Append fld
fld.Properties("Required").Value = True
fld.Properties("AllowZeroLength").Value = False
Set fld = tbl.CreateField("Count", dbInteger)
tbl.Fields.Append fld
fld.Properties("Required").Value = True
fld.Properties("AllowZeroLength").Value = False
'Populate the CSP_Level (Rec_Ref, Location_Name and Event_Title are fields within the table
Function CSP_Level(Rec_Ref, Location_Name, Event_Title)
If Rec_Ref > "" Then CSP_Level = "Project"
ElseIf Location_Name > "" And Event_Title Is Null Then CSP_Level = "Site"
ElseIf Event_Title > "" Then CSP_Level = "Event"
End If
CleanUp:
Set fld = Nothing
Set tbl = Nothing
Set db = Nothing
Exit Function
ErrHandler:
MsgBox "Error in addFieldToTbl( )." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp
End Function
End Sub
Thank you for any help that you can give,
sparkes84