473,387 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using a variable to identify a field.

Here is My code for starters:

Expand|Select|Wrap|Line Numbers
  1. Select Case varMonth
  2.         Case Is = 1
  3.             varLongMonth = "January"
  4.         Case Is = 2
  5.             varLongMonth = "Feburary"
  6.         Case Is = 3
  7.             varLongMonth = "March"
  8.         Case Is = 4
  9.             varLongMonth = "April"
  10.         Case Is = 5
  11.             varLongMonth = "May"
  12.         Case Is = 6
  13.             varLongMonth = "June"
  14.         Case Is = 7
  15.             varLongMonth = "July"
  16.         Case Is = 8
  17.             varLongMonth = "August"
  18.         Case Is = 9
  19.             varLongMonth = "September"
  20.         Case Is = 10
  21.             varLongMonth = "October"
  22.         Case Is = 11
  23.             varLongMonth = "November"
  24.         Case Else
  25.             varLongMonth = "December"
  26.     End Select
  27.     txttest = varLongMonth
  28.  
  29.     Dim db As DAO.Database
  30.     Dim rs1 As DAO.Recordset
  31.  
  32.     Set db = CurrentDb()
  33.     Set rs1 = db.OpenRecordset("Query_2")
  34.     rs1.MoveFirst
  35.  
  36.     Do Until rs1.EOF
  37.         If rs1![ID] = Val(ID) Then
  38.             NumOrdTotal = rs1!varLongMonth + Val(NumOrd)
  39.             rs1.Edit
  40.             rs1!varLongMonth = NumOrdTotal
  41.             rs1.Update
  42.         End If
  43.         rs1.MoveNext
  44.     Loop
  45.  
  46.   rs1.Close
  47.   Set rs1 = Nothing
  48.   Set db = Nothing
  49.  
At the line, NumOrdTotal = rs1!varLongMonth + Val(NumOrd) , I get an error saying: Item not found in Collection.

I believe I am getting this error because where the field belongs at rs1!"fieldName" I have a variable. I need to be able to use this variable to identify the field depending on certain criteria. How can I use this variable to do that?
Feb 15 '07 #1
7 2134
NeoPa
32,556 Expert Mod 16PB
Try :
Expand|Select|Wrap|Line Numbers
  1. rs1.Fields(varLongMonth)
Feb 15 '07 #2
NeoPa
32,556 Expert Mod 16PB
Try using this to convert month number to string :
Expand|Select|Wrap|Line Numbers
  1. varLongMonth = Format(CDate("25/" & varMonth & "/1999"),"mmmm")
Feb 15 '07 #3
Try :
Expand|Select|Wrap|Line Numbers
  1. rs1.Fields(varLongMonth)
Thanks NeoPa that did the trick, but now I got another probelm, WOOHOO. I can not get it to enter data into other records, just the first record. It is not giving any errors it runs it like its working. So its just some little thing.

Updated Code:
Expand|Select|Wrap|Line Numbers
  1.     Select Case varMonth
  2.         Case Is = 1
  3.             varLongMonth = "January"
  4.         Case Is = 2
  5.             varLongMonth = "Feburary"
  6.         Case Is = 3
  7.             varLongMonth = "March"
  8.         Case Is = 4
  9.             varLongMonth = "April"
  10.         Case Is = 5
  11.             varLongMonth = "May"
  12.         Case Is = 6
  13.             varLongMonth = "June"
  14.         Case Is = 7
  15.             varLongMonth = "July"
  16.         Case Is = 8
  17.             varLongMonth = "August"
  18.         Case Is = 9
  19.             varLongMonth = "September"
  20.         Case Is = 10
  21.             varLongMonth = "October"
  22.         Case Is = 11
  23.             varLongMonth = "November"
  24.         Case Else
  25.             varLongMonth = "December"
  26.     End Select
  27.     txttest = varLongMonth
  28. '========================================================================================
  29.     Dim db As DAO.Database
  30.     Dim rs1 As DAO.Recordset
  31.  
  32.     Set db = CurrentDb()
  33.     Set rs1 = db.OpenRecordset("CartUse")
  34.     rs1.MoveFirst
  35.  
  36.     Do Until rs1.EOF
  37.         If rs1.Fields(ID) = Val(ID) Then
  38.             NumOrdTotal = Val(rs1.Fields(varLongMonth)) + Val(varNumOrd)
  39.             txttest2 = Val(NumOrdTotal)
  40.             rs1.Edit
  41.             rs1.Fields(varLongMonth) = Val(NumOrdTotal)
  42.             rs1.Update
  43.         End If
  44.         rs1.MoveNext
  45.     Loop
  46.  
  47.   rs1.Close
  48.   Set rs1 = Nothing
  49.   Set db = Nothing
  50.  
Feb 16 '07 #4
NeoPa
32,556 Expert Mod 16PB
You need to post this as a new question as this is unrelated to this one.
Try to explain exactly what you're after a little more clearly though. If I'd understood the question I would have tried to answer it anyway.
Feb 16 '07 #5
NeoPa
32,556 Expert Mod 16PB
Try using this to convert month number to string :
Expand|Select|Wrap|Line Numbers
  1. varLongMonth = Format(CDate("25/" & varMonth & "/1999"),"mmmm")
BTW. I'm pleased that helped and thanks for the feedback - always valuable.
Did you notice this post (#3 quoted)? It should help you to get rid of a whole hunk of code.
Feb 16 '07 #6
Alright I moved it here: Using a Do Until Loop to search for and modify data in a table And also I will probably change that case statement to the code you gave me after I get it writing data and working better first.
Feb 16 '07 #7
NeoPa
32,556 Expert Mod 16PB
Cool - Nicely done :)
Feb 17 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: N. David | last post by:
In bash, you can include a variable as part of a filename. For example: date=$(date) cat log.txt > $date"_log.txt" Which, when setting the right options in date, creates a file named...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
5
by: tino | last post by:
Years ago there used to be a feature in dBase and FoxPro where the content of a variable could be used a variable name: Var1 = "Var2" Var2 = blank &Var1 = "Hallo" this way Var2 would get...
1
by: Alex | last post by:
Given the following code: Private Sub ButtonShowTheIntValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonShowTheIntValue.Click ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.