OK Guys&Gals,
I’ve been trying to get an answer in some way, shape, or form, via my last few posts, but I think I’m on the wrong track… So, I’m gonna back up and ask a broader question.
I have a bunch of fields in a table all named sequentially: MM1YY, MM2YY, MM3YY….MM12YY.
I also have this table linked to a form, where all of these fields need to be updated, based upon the Value entered in the first field… MM1YY.
Meaning if the user enters 08/05 for MM1YY, I want MM2YY, MM3YY, MM4YY…etc… To be updated 09/05, 10/05, 11/05… for all remaining fields. (BTW: They enter the value as a string, not a date field)
Without going into database design (as I am not the person who created the database), I was wondering if I can programmatically auto fill these values. I mean… I know I can. I already did so with a couple of case statements that loops through each possibility and fills in the value for each field… But what I want is a better way of doing this, where I don’t have to write out all the the names of the fields by hand. My thought was, it’s fine if there are 12 fields, but what if there were 25?...Or more?
I’d want to be able to loop through and post the values to each of the sequential fields in the form, without having to hard code each field name into the code, but I can’t seem to figure out how?
I’ve looked up a bunch of stuff on the subject but seem to be running into dead ends… I thought using collections might be the answer, but that seems to just give me an array which can't update the values in the table… maybe a record set, which updates the table, might work… however, at this point, I am lost as to how to reference each field and place the value in it.
Right now my code works and it looks like this:
Private Sub MM1_YY_LostFocus()
Dim strDate, strCell As String
Dim intMM, intYY, intCount, i As Integer
strDate = Me.MM1YY
intMM = CInt(Left(strDate, 2))
intYY = CInt(Right(strDate, 2))
Do While intCount < 13
If intMM < 12 Then
Select Case intMM
Case 1
strCell = "02/0" & intYY
Case 2
strCell = "03/0" & intYY
Case 3
strCell = "04/0" & intYY
Case 4
strCell = "05/0" & intYY
Case 5
strCell = "06/0" & intYY
Case 6
strCell = "07/0" & intYY
Case 7
strCell = "08/0" & intYY
Case 8
strCell = "09/0" & intYY
Case 9
strCell = "10/0" & intYY
Case 10
strCell = "11/0" & intYY
Case 11
strCell = "12/0" & intYY
End Select
Else
strCell = "01/0" & intYY + 1
intMM = 0
intYY = intYY + 1
End If
intMM = intMM + 1
Select Case intCount
Case 2
Me.MM2YY = strCell
Case 3
Me.MM3YY = strCell
Case 4
Me.MM4YY = strCell
Case 5
Me.MM5YY = strCell
Case 6
Me.MM6YY = strCell
Case 7
Me.MM7YY = strCell
Case 8
Me.MM8YY = strCell
Case 9
Me.MM9YY = strCell
Case 10
Me.MM10YY = strCell
Case 11
Me.MM11YY = strCell
Case 12
Me.MM12YY = strCell
End Select
intCount = intCount + 1
Loop
Me.Refresh
End Sub
Even though it works, it seems like it is a lot of lines for the little that it actually does… BTW: I know with the first case statement, there’s probably a more efficient way of writing that as well… but I was working so hard on the one aspect, I kind of got a bit lazy on the other…while I wait for an answer, maybe I’ll figure out something better than a case statement for that section…
So… Maybe you bigger brained people can answer: what is a better way of doing this?
Thanks,
Gary
I’ve been trying to get an answer in some way, shape, or form, via my last few posts, but I think I’m on the wrong track… So, I’m gonna back up and ask a broader question.
I have a bunch of fields in a table all named sequentially: MM1YY, MM2YY, MM3YY….MM12YY.
I also have this table linked to a form, where all of these fields need to be updated, based upon the Value entered in the first field… MM1YY.
Meaning if the user enters 08/05 for MM1YY, I want MM2YY, MM3YY, MM4YY…etc… To be updated 09/05, 10/05, 11/05… for all remaining fields. (BTW: They enter the value as a string, not a date field)
Without going into database design (as I am not the person who created the database), I was wondering if I can programmatically auto fill these values. I mean… I know I can. I already did so with a couple of case statements that loops through each possibility and fills in the value for each field… But what I want is a better way of doing this, where I don’t have to write out all the the names of the fields by hand. My thought was, it’s fine if there are 12 fields, but what if there were 25?...Or more?
I’d want to be able to loop through and post the values to each of the sequential fields in the form, without having to hard code each field name into the code, but I can’t seem to figure out how?
I’ve looked up a bunch of stuff on the subject but seem to be running into dead ends… I thought using collections might be the answer, but that seems to just give me an array which can't update the values in the table… maybe a record set, which updates the table, might work… however, at this point, I am lost as to how to reference each field and place the value in it.
Right now my code works and it looks like this:
Private Sub MM1_YY_LostFocus()
Dim strDate, strCell As String
Dim intMM, intYY, intCount, i As Integer
strDate = Me.MM1YY
intMM = CInt(Left(strDate, 2))
intYY = CInt(Right(strDate, 2))
Do While intCount < 13
If intMM < 12 Then
Select Case intMM
Case 1
strCell = "02/0" & intYY
Case 2
strCell = "03/0" & intYY
Case 3
strCell = "04/0" & intYY
Case 4
strCell = "05/0" & intYY
Case 5
strCell = "06/0" & intYY
Case 6
strCell = "07/0" & intYY
Case 7
strCell = "08/0" & intYY
Case 8
strCell = "09/0" & intYY
Case 9
strCell = "10/0" & intYY
Case 10
strCell = "11/0" & intYY
Case 11
strCell = "12/0" & intYY
End Select
Else
strCell = "01/0" & intYY + 1
intMM = 0
intYY = intYY + 1
End If
intMM = intMM + 1
Select Case intCount
Case 2
Me.MM2YY = strCell
Case 3
Me.MM3YY = strCell
Case 4
Me.MM4YY = strCell
Case 5
Me.MM5YY = strCell
Case 6
Me.MM6YY = strCell
Case 7
Me.MM7YY = strCell
Case 8
Me.MM8YY = strCell
Case 9
Me.MM9YY = strCell
Case 10
Me.MM10YY = strCell
Case 11
Me.MM11YY = strCell
Case 12
Me.MM12YY = strCell
End Select
intCount = intCount + 1
Loop
Me.Refresh
End Sub
Even though it works, it seems like it is a lot of lines for the little that it actually does… BTW: I know with the first case statement, there’s probably a more efficient way of writing that as well… but I was working so hard on the one aspect, I kind of got a bit lazy on the other…while I wait for an answer, maybe I’ll figure out something better than a case statement for that section…
So… Maybe you bigger brained people can answer: what is a better way of doing this?
Thanks,
Gary