Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Using variables to write data to an Access file

 
   Database Forums (Home) -> Visual Basic -> DAO RSS
Next:  clipboard  
Author Message
robert.mcinerney

External


Since: Feb 18, 2007
Posts: 2



(Msg. 1) Posted: Sun Feb 18, 2007 6:45 pm
Post subject: Using variables to write data to an Access file
Archived from groups: microsoft>public>vb>database>dao (more info?)

Hi all,

I have an MS Access database with just one table. In that table are
several dozen columns, each one called Variable1, Variable2,
Variable3, etc.

Is there any way to use a For/Next loop to write information to this
table? For example, if I wanted to write data to just one variable, I
could use this: MyDatabase!Variable1 = [whatever value].

What I'm trying to do is write all of the variables at once, using
something like this:

For b = 1 to 100
MyDatabase!Variable"(b)" = [whatever value].
Next b

Obviously this code doesn't work, but I'm wondering whether someone
knows how I can accomplish this.

Thanks,
Rob

 >> Stay informed about: Using variables to write data to an Access file 
Back to top
Login to vote
Norm Cook1

External


Since: Jun 27, 2004
Posts: 24



(Msg. 2) Posted: Mon Feb 19, 2007 7:28 am
Post subject: Re: Using variables to write data to an Access file [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

wrote in message

> Hi all,
>
> I have an MS Access database with just one table. In that table are
> several dozen columns, each one called Variable1, Variable2,
> Variable3, etc.
>
> Is there any way to use a For/Next loop to write information to this
> table? For example, if I wanted to write data to just one variable, I
> could use this: MyDatabase!Variable1 = [whatever value].
>
> What I'm trying to do is write all of the variables at once, using
> something like this:
>
> For b = 1 to 100
> MyDatabase!Variable"(b)" = [whatever value].
> Next b
>
> Obviously this code doesn't work, but I'm wondering whether someone
> knows how I can accomplish this.
>
> Thanks,
> Rob

This is a DAO group, so assuming DAO

Say your query asks for any 3 fields, something like
set RS=DB.OpenRecordSet("Select Variable1,Variable12,Variable35 From
YourTable")

Then the returned recordset will have 3 fields as follows
RS.Field(0) ' Variable1
RS.Field(1) ' Variable12
RS.Field(2) ' Variable35 'note the order of the fields in the query

Similarly, if your query gets all the fields
set RS=DB.OpenRecordSet("Select * From YourTable") then you
can loop thru with something like
For i = 0 to RS.Fields.Count - 1
RS.Edit
RS.Fields(i).Value = [whatever value] 'likely an array of data
RS.Update
Next 'note: This is updating only 1 row, but all the fields

This will allow you to give your Fields more descriptive names
which will preserve your sanity.

 >> Stay informed about: Using variables to write data to an Access file 
Back to top
Login to vote
Henning

External


Since: Jan 15, 2006
Posts: 20



(Msg. 3) Posted: Mon Feb 19, 2007 2:20 pm
Post subject: Re: Using variables to write data to an Access file [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

skrev i meddelandet

> Hi all,
>
> I have an MS Access database with just one table. In that table are
> several dozen columns, each one called Variable1, Variable2,
> Variable3, etc.
>
> Is there any way to use a For/Next loop to write information to this
> table? For example, if I wanted to write data to just one variable, I
> could use this: MyDatabase!Variable1 = [whatever value].
>
> What I'm trying to do is write all of the variables at once, using
> something like this:
>
> For b = 1 to 100
> MyDatabase!Variable"(b)" = [whatever value].
> Next b
>
> Obviously this code doesn't work, but I'm wondering whether someone
> knows how I can accomplish this.
>
> Thanks,
> Rob
>
If using SQL queries, this works that way (filter out what you need)

With SkoRent(sc)
tmp = .Antal
If .QueNr > 0 Then
strSQL = "UPDATE orders SET " _
& "pnr=""" & .PersNr & """, " _
& "tel=""" & .Phone & """, " _
& "adress=""" & .Adress & """, " _
& "comment=""" & .Comment & """, " _
& "datum=""" & .Datum & """, " _
& "antal=" & .Antal
For i = 1 To 10
If tmp > 0 Then
stmp = "sko" & Format(i)
strSQL = strSQL & ", " & stmp & "=" & .ShoeNr(i)
tmp = tmp - 1
Else
stmp = "sko" & Format(i)
strSQL = strSQL & ", " & stmp & "=" & 0
End If
Next
strSQL = strSQL & " WHERE ordernr=" & .QueNr & ";"
Else
strSQL = "INSERT INTO orders VALUES(" _
& .QueNr & ", " _
& "'" & .PersNr & "', " _
& "'" & .Phone & "', " _
& "'" & .Adress & "', " _
& "'" & .Comment & "', " _
& "'" & .Datum & "', " _
& .Antal
For i = 1 To 10
If tmp > 0 Then
strSQL = strSQL & ", " & .ShoeNr(i)
tmp = tmp - 1
Else
strSQL = strSQL & ", " & 0
End If
Next
strSQL = strSQL & ", " & 0 & ");"
End If
End With

conn.Execute strSQL, , adExecuteNoRecords


hth /Henning
 >> Stay informed about: Using variables to write data to an Access file 
Back to top
Login to vote
robert.mcinerney

External


Since: Feb 18, 2007
Posts: 2



(Msg. 4) Posted: Mon Feb 19, 2007 2:52 pm
Post subject: Re: Using variables to write data to an Access file [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

These are excellent suggestions. Thanks to both for your help - job
accomplished.

Cheers,
Rob
 >> Stay informed about: Using variables to write data to an Access file 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Replacing SaveSetting with saving to a parameter file (Acc.. - I am trying to replace VB6 SaveSetting command, to use a parameter file rather than the registry. I want to make the equivalent of : SaveSetting appname, section, key, setting where I pass "key" and "setting" to new function M...

Error conneecting to Access "Cannot Start Your Application.. - I can connect to the database manually, by starting it and entering my login info, but I still get that error. Here is my code: Sub SetUpConnection() On Error GoTo ErrHandler Set adoCN = New Connection adoCN.Provider =..

Getting data from Access 2003 in Vb6 with DAO - Is this possible? What version of DAO do I need and where do I get it from. TIA!

Delay in data appearing in access table - I have an app written in VB6. using the DAO 2.5/3.5 Compatability Library I have a problem with a report that collates some data and writes it into a table and then runs a crystal report (v9) to report on the data. Sometimes the report displayed no dat...

problem w/accessing data in a memo field in an Access 2.0 .. - I am having all sorts of trouble trying to read .bmp image data from a memo field in an Access 2.0 database. I'm in the process of upgrading one of our customers ancient Windows 3.1 systems to a new one, running the slightly less ancient Windows 2000...
   Database Forums (Home) -> Visual Basic -> DAO All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]