camerontaylor
New member
- Local time
- Today, 15:08
- Joined
- May 11, 2021
- Messages
- 29
I am very very new to VBA, so I apologize if I am going about this completely wrong.
I need to create a form which can Create, Update, and Delete records from a table. Each record will have a title field, a description field, a user field, a start date field, an estimated end date field, an actual end date field, and a project status field. I am currently working on the Create portion, and am not worrying about the other ones.
What I need to do, is on the click of the create, I need to create a new record. I want to open a form, have a user fill some of the fields listed above, and then save it to the record. I then need to look at the data just entered, and extract the date it was created. I also need to determine which number entry it is in the month (i.e. first entry, second entry, etc.), and whether it was created in the same month as the last entry (if it was created in a new month from the last entry, then restart the monthly counter at 1)(it also can't just reset on the first day of the month, because there's no guarantee that an entry will be made on the first day of the month).
From the date, and the monthly counter, I need to create a specific project number, which has the format "EPYYMM-##" where the ## is the monthly count (eg. the third project created in May 2021 would be EP2105-03). We don't expect to have more than 99 new projects per month, so number of digits at the end isn't a concern.
I could really use some help in designing my VBA code for this. I have written one Sub so far (attached below), but it isn't working, so any help is greatly appreciated.
I need to create a form which can Create, Update, and Delete records from a table. Each record will have a title field, a description field, a user field, a start date field, an estimated end date field, an actual end date field, and a project status field. I am currently working on the Create portion, and am not worrying about the other ones.
What I need to do, is on the click of the create, I need to create a new record. I want to open a form, have a user fill some of the fields listed above, and then save it to the record. I then need to look at the data just entered, and extract the date it was created. I also need to determine which number entry it is in the month (i.e. first entry, second entry, etc.), and whether it was created in the same month as the last entry (if it was created in a new month from the last entry, then restart the monthly counter at 1)(it also can't just reset on the first day of the month, because there's no guarantee that an entry will be made on the first day of the month).
From the date, and the monthly counter, I need to create a specific project number, which has the format "EPYYMM-##" where the ## is the monthly count (eg. the third project created in May 2021 would be EP2105-03). We don't expect to have more than 99 new projects per month, so number of digits at the end isn't a concern.
I could really use some help in designing my VBA code for this. I have written one Sub so far (attached below), but it isn't working, so any help is greatly appreciated.
Code:
Option Compare Database
Private Sub createNewProject_Click()
incMonthNum
End Sub
'-------------------------------------------------------------------------
'This will increment the monthly count by 1 for each new project entry,
'and will reset the count to 1 if the new entry is created in a new month
'to the previous entry
'-------------------------------------------------------------------------
Private Sub incMonthNum()
Dim d As Database, r As Recordset, monthNum As Integer, lastMonth As Field
Set d = CurrentDb()
Set r = d.OpenRecordset("Project List")
Set lastMonth = DLast("r.Fields('Month')", "r")
If lastMonth = month(Date) Then
Set monthNum = DLast("r.Fields('Month Count')", "r") + 1 'this is where im getting an error right now (says "Object Required")
Else
Set monthNum = 1
End If
DoCmd.RunSQL ("INSERT INTO [Project List] ([Month Count],Month) VALUES (" & monthNum & Date & ")")
r.Close
End Sub