For some reason I cannot make it work, so here is the code I'm using.
But first I'll explain my database a bit. At this moment, I have one
table with thousands of records. Each record contains information
about a contract. All the contracts have a startdate and an enddate.
For example contract could start 1.1.2007 and end 31.1.2007, or start
1.4.2007 and end 30.6.2007.
I fetch the records from a server once a day. After putting the
records into the table I create columns to the table starting from
the first day of the first month of the year (so now the first added
column is '1.1.2007') and continuing for 6 years. So I have a table
that contains tens of columns: contract information (for example
name, startdate, enddate), '1.1.2007', '1.2.2007', ..., '1.12.2012'
The month-columns (like '1.1.2007') are made using sql like this:
Dim pvmnyt As Date 'current date
Dim vuosi As Integer 'year
Dim jono As String 'string that contains column name information
Dim kuukausi As Integer 'month
Dim i As Integer
Dim strSql As String
pvmnyt = Date
vuosi = Year(pvmnyt)
kuukausi = 1
jono = "1/" & kuukausi & "/" & vuosi
'here I create the columns
For i = 0 To 71
strSql = "ALTER TABLE Sopimustaulu ADD ' " & jono & "' double);"
DoCmd.RunSQL (strSql)
'and here I change the "date" of the column
If kuukausi = 12 Then
kuukausi = 1
vuosi = vuosi + 1
Else
kuukausi = kuukausi + 1
End If
jono = "1/" & kuukausi & "/" & vuosi
Next
Ok, so now I have the columns. For some reason sql puts '' around the
column name, so my jono string is 1.1.2007 but the column name is
'1.1.2007'.
Then I need to put information into these columns. I'm just starting
to do this, so my sub isn't finished yet. The idea is that I pick the
start date and the end date of the contract and as long as the start
date isn't the end date I put price information into the right cells.
My sub is really simple right now, but there are prices in different
currencies, so I need to do lots of other things to the sub too. And
of course the contracts are of different lenghts. In this code I
don't have to change price information, because the currency I'm
looking at is euro.
Sub Sopimustaulu_With_Prices()
'price information into table Sopimustaulu
Dim rs As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset
Dim salku As Date 'starting date
Dim sloppu As Date 'ending date (for the contract)
Dim shinta As Double 'price for the contract
Dim svaluutta As String 'currency information (what currency)
Dim strjono As String
'I open my contract table
rs.Open "Sopimustaulu", CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
'and then I open a table where i can find prices (but I don't need
this yet, later I do)
rs2.Open "Hintataulu", CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
rs.MoveFirst
Do While rs.EOF = False
'I pick the information I need from the recordset
salku = rs![STARTDATE]
sloppu = rs![ENDDATE]
shinta = rs![PRICE]
svaluutta = rs![CURRENCYCODE]
'if the contract is valid for only one month
If Year(salku) = Year(sloppu) And Month(salku) = Month(sloppu) Then
If svaluutta = "EUR" Then
shinta = rs![PRICE]
strjono = "'1/" & Month(salku) & "/" & Year(salku) & "'"
'easier to see the ': strjono = " '1/ " & Month(salku) & "/" &
Year(salku) & " ' "
'these don't work
'rs.Fields(strjono).Value = shinta
'rs.Fields([ " & strjono & " ]).Value = shinta
'rs.Fields ("[" & yourFieldName & "]")
'if I insert the found price into the table like this
'(I made a test column called F16)
rs![F16] = shinta
'I get all the right price information into the F16 -column
End If
End If
rs.Update
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
rs2.Close
Set rs2 = Nothing
End Sub
Ok, there it was.. I hope you get the idea and can tell me why it
doesn't work. It is really great that there is a place like this
where one can go and ask and get really helpful information! So
thanks for that!
And by the way, I need to put the price
information into the table like that because this is only the first
step of my project, and I need information in that form so that I can
go on and create other tables based to this information.