inserting a range of numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello everyone. I'm new to vba so please bear with me. I would like to
insert a range of numbers based on user input. For example, if the user puts
5 to 60, it should insert: 5,6,7,8... in tblFiles.FIleNumber. I have
something like this but it's not working. It has to do with my counter I
think. I'm using Access 2003. Thanks for all help.

Private Sub cmdSave_Click()
Dim FileCount as integer
Dim Counter as integer

FileCount= Me.txtTo.Value - Me.txtFrom.Value
Counter = 1

Do While Counter <= FileCount
RunSQL = "INSERT INTO tblFiles(FileNumber) Values (Counter)"
Counter = Counter + 1
Loop

End Sub
 
You had the right concept.

Try something like:

Private Sub cmdSave_Click()
Dim intLBound as integer
Dim intUBound as integer
Dim intCounter as integer

intLBound = Me.txtFrom.Value
intUBound = Me.txtTo.Value

For intCounter = intLBound to intUBound
'Run your code here
RunSQL = "INSERT INTO tblFiles(FileNumber) Values (intCounter)"
Next intCounter

End Sub

Daniel
 
If you are going to use that code, you need to add the counter value to sql
string and you need to execute the string.

Private Sub cmdSave_Click()
Dim Counter as integer
Counter = Me.TxtFrom

Do While Counter <= Me.TxtTo
DoCmd.RunSQL = "INSERT INTO tblFiles(FileNumber) Values (" & Counter & ")"
Counter = Counter + 1
Loop

End Sub

I can't comment on how appropriate the above is since I don't know what else
you are doing with this set of numbers.

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
Thanks John! This worked. I just wanted to store the numbers in table for
future use.

I do have another question. Currently, I get a promt asking me if it's okay
to append 1 record and I have to hit okay. I don't want to have to say okay
200 times if I'm inserting that amount of number. How do I eliminate this
promt to just one in the end? Agan thank you very much..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top