type in to a date range which creates records

hello

Registered User.
Local time
Today, 05:57
Joined
Aug 15, 2012
Messages
14
Hi.
Hi want a form:
Name , start date , end date

After you have typed in the data above, the data should converts to a record for each year in a table.

Example:

Form:
Sarah 2003-02-01 2004-02-01

Table:

Sarah 2003
Sarah 2004
 
Create the table as mentioned, create a form bound on the table OR create a form as mentioned with its recordsource as Table and then in the Form before update event have the following..
Code:
Private Form_BeforeUpdate(cancel As Integer)
    Me.[NameFieldName]=Me.NameFormControlName
    Me.[DateFieldName]=Year(startDateFormControlName)
End Sub
 
I would have a button to execute it but you can add them to the table by using:
Code:
Private Sub YourButtonNameHere_Click()
   Dim intStartYear As Integer
   Dim intEndYear As Integer
   Dim intCount As Integer
   Dim strSQL As String

      intStartYear = Year(Me.[Start Date])
      intEndYear = Year(Me.[End Date])

      For intCount = intStartYear To intEndYear
           strSQL = "INSERT INTO YourTableNameHere ([Name], [Year]) Values (" & Chr(34) & Me.[Name] & Chr(34) & ", " & intCount & ")"
           CurrentDb.Execute strSQL, dbFailOnError          
      Next
End Sub

Now, using NAME as a field name is bad. Using it for any object name is bad. It is an Access Reserved Word and is one of the worst to use because every object has a .Name property. So, I would not use it. Also Year is an Access Reserved Word. Notice how I had to put the reserved words in square brackets. You can do that but it still may come back to bite you in the end. Also, the CHR(34) is a double quote.
 

Users who are viewing this thread

Back
Top Bottom