MandyHead1
New member
- Local time
- Today, 10:13
- Joined
- Sep 5, 2018
- Messages
- 8
Hi there,
I am trying to create a forecasting database and I would like a button on my form (frmProjects) to Automatically fill in several records in a table (tblProjForecast) with dates in a date range specified in the form. The form (based on table 'tbl Projects') has the following fields:
[*]ProjectStatus
[*]ProjectValue
[*]EstStartDate
[*]EstFinishDate
[*]EstNoMonths (Calculated( EstFinishDate - EstStartDate) / 30
and the target Table, tblProjForecast has a few fields, but i only want the field FcstDate(s) to be entered.
I also have a table called tblMonths with a field 'FcstMonth' where I have manually entered months in the calendar year ie 1/8/18, 1/9/18 etc.
Example: EstStartDate = 1/8/18, EstFinishDate = 1/10/18.
I want the button to add records for 1/8/18, 1/9/18, 1/10/18
Please note, I am new to VBA so please be patient with me. I have looked up similar issues and came across one that I tried to adapt for my own circumstance. It worked until it got to the INSERT INTO bit, where it said 'Syntax error'.
If anyone can advise what i've done wrong, it will be greatly appreciated.
thanks,
MandyHead1
I am trying to create a forecasting database and I would like a button on my form (frmProjects) to Automatically fill in several records in a table (tblProjForecast) with dates in a date range specified in the form. The form (based on table 'tbl Projects') has the following fields:
[*]ProjectStatus
[*]ProjectValue
[*]EstStartDate
[*]EstFinishDate
[*]EstNoMonths (Calculated( EstFinishDate - EstStartDate) / 30
and the target Table, tblProjForecast has a few fields, but i only want the field FcstDate(s) to be entered.
I also have a table called tblMonths with a field 'FcstMonth' where I have manually entered months in the calendar year ie 1/8/18, 1/9/18 etc.
Example: EstStartDate = 1/8/18, EstFinishDate = 1/10/18.
I want the button to add records for 1/8/18, 1/9/18, 1/10/18
Please note, I am new to VBA so please be patient with me. I have looked up similar issues and came across one that I tried to adapt for my own circumstance. It worked until it got to the INSERT INTO bit, where it said 'Syntax error'.
Code:
Option Compare Database
Private Sub btnAddDates_Click()
Dim strSQL As String
'Validate fields are not Null
If IsNull(Me.EstStartDate) Then
MsgBox "EstStartDate is Missing", vbOKOnly + vbCritical, "Error"
Me.EstStartDate.SetFocus
ElseIf IsNull(Me.EstFinishDate) Then
MsgBox "EstFinishDate is Missing", vbOKOnly + vbCritical, "Error"
Me.EstFinishDate.SetFocus
ElseIf IsNull(Me.ProjectValue) Then
MsgBox "ProjectValue is Missing", vbOKOnly + vbCritical, "Error"
Me.ProjectValue.SetFocus
Else
'build the SQL Statement
strSQL = "INSERT INTO tblProjForecast ( ForecastDate)"
strSQL = strSQL & " FROM Dates WHERE (((tblMonths.FcstMonth)>[Forms]![tblProjForecast]![EstStartDate] And (tblMonths.FcstMonth)<=[Forms]![tblProjForecast]![EstFinishDate]))"
'Run the SQL Statement
DoCmd.RunSQL (strSQL)
End If
Me.Requery
End Sub
If anyone can advise what i've done wrong, it will be greatly appreciated.
thanks,
MandyHead1