stupid type mismatch that doesn't make sense

bigwheat
10-20-2000, 01:59 PM
I am getting a type mismatch error when trying to write info from my form to an already existing table in an access database. Here is the code:

Function addtodatabase()
Set rs = db.OpenRecordset("SELECT testkey.* FROM
testkey")
With rs
.AddNew
.Fields("testid").Value = txttestid
.Fields("numquest").Value = txtnumq
.Update
.Close
End With
End Function

Private Sub cmdadd_Click()
Call addtodatabase(txttestid, txtnumq)
End Sub

I have code to open the database in the formload procedure. I know that it is probably some stupid mistake that I am just not seeing. Any help would be greatly appreciated.

Also, is it possible to write to a db field as an array?
For example, I have fields in a table named q1 through q50. My life would much easier if I could put it in a for/next loop and add the number there. I tried it this way, and it didn't work: (cans() are the txtboxes on my form)

for x = 1 to 50
.Fields("q"&x).Value = cans(x)
next x


Thanks again for any help!

BillSoo
10-20-2000, 02:38 PM
Well, for one thing, you don't have any parameters declared in the function addtodatabase.

Also, could you post the design of the table.

Also, I haven't actually seen the format "testkey.* " before; what does that do?

Finally, you can refer to fields by index (at least in DAO you can; I don't use ADO yet). Something like:
rs(i%) = s$
I think the fields are arranged in the order that they are created. I can't remember if it is 0 or 1 based though...

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

bigwheat
10-20-2000, 02:51 PM
The "testkey.*" is an sql statement that returns all the fields in the database. Or so I read somewhere....

The table has a textbox, txttestid, which relates to the primary key on the table. Another textbox, txtnumq, and then fifty more textboxes, starting with cans(1) through cans(50). Then a command button called cmdadd. The txttestid should relate to testid field in db, txtnumq - numquest field in db, cans(1 to 50) - q1 to q50 in db.

I will try referring to the fields by index.

I am not sure what you mean as far as parameters declared in the function.

I admit that I am new to to working with vb and db. I have found a number of very helpful how-to's online, and I kinda feel like I'm getting close to what I am trying to do, but I guess I need to go get some books.

Thanks for you help!

BillSoo
10-20-2000, 03:04 PM
I usually use "SELECT * FROM" instead.

When you call your function, you say:

Call addtodatabase(txttestid, txtnumq)

but in your declaration, you make no mention of the txttestid or txtnumq parameters.

So you have to declare your input parameters. Something like:

sub addtodatabase(txttestid as string,txtnumq as string)



"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

bigwheat
10-20-2000, 03:10 PM
great. i'll try that. Thanks for your help!

The way I had done the sql statement was what I had copied and pasted directly from access. I saw the way you suggested in a tutorial I read.

Thanks again!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum