someone help im about to kill myself - Yet more Databases and Active X

Posted by Ishnaf on Tue 01 Oct 2002 02:14 PM — 5 posts, 19,290 views.

Australia #0
Why wont this work? Im dying over here :P

This is supposed to add info (which i am getting okay from mushclient) to a database. It doesnt. Can someone plllllleeeeaaaaassssseeeeeee tell me why......

Sub GetStuff(gworld As World)

dim item, iteminfo, 'etc... i dim everything

'i have lots of getvariables here usually - you dont need to see this its working fine :)

'the next bit wont work :|

'~~~ADD INFO INTO THE DATABASE~~~'
'this is supposed to open ITEMDB recordset:

Dim Itemdb As Database
Dim RecSet As Recordset
'okay, the db below (newdatabase.mdb) has a table called "items"
'im attempting to open it :)
Set itemdb = OpenDatabase("C:\Program Files\MUSHclient\ishnaf\DBproject\newdatabase.mdb")
Set RecSet = itemdb.OpenRecordset("Items", dbOpenDynaset)


'now im trying to add a record to this record source
With RecSet
'Set it to Add mode
.AddNew

.Fields("Item").Value = Item
.Fields("ItemType").Value = itemtype
.Fields("ItemIs").Value = itemis
.Fields("ForgeLevel").Value = forgelvl
.Fields("DiceNo").Value = diceno
.Fields("DiceSize").Value = dicesize
.Fields("Weight").Value = weight
.Fields("Value").Value = valuee
.Fields("Rent").Value = rent
.Fields("ACApply").Value = acapply
.Fields("Armor").Value = Armor
.Fields("CON").Value = con
.Fields("INTEL").Value = intel
.Fields("WIS").Value = wis
.Fields("DEX").Value = dex
.Fields("CHA").Value = Cha
.Fields("STR").Value = stre
.Fields("Hitroll").Value = hitroll
.Fields("Damroll").Value = damroll
.Fields("maxhit").Value = maxhit
.Fields("maxmana").Value = maxmana
.Fields("Uselevel").Value = uselevel
.Fields("Age").Value = age

'Update it
.Update
'Close it
.Close
End With

Set db = Nothing
'to send something to world to see if the script makes it to here
gworld.Note "The Item Has been added. Perhaps :P"
End Sub


Any ideas? Or am i insane?
USA #1
With RecSet
'Set it to Add mode
.AddNew

.Fields("Item").Value = Item
...

You sure this works? Seems to me that you need to .AddNew before you do anything 'With' it. i.e.

RecSet.AddNew
With RecSet
.Fields("Item").Value = Item
...

It would seem to me that you can't set the fields to an empty record set, so until you do AddNew, there are no fields to put anything into. In most cases stuff like AddNew don't define a mode, but create a new record or object. However that is only a 'major' guess.
Australia Forum Administrator #2
Quote:

Seems to me that you need to .AddNew before you do anything 'With' it.


With is just a VB shortcut, at compile time. It has nothing to do with executing the code. eg.


x.AddNew


and


With x
.AddNew
End With


are identical. However using "with" in a long sequence (as above) saves typing the object name repetitively on each line.

As for why the code doesn't work, I'll try it tomorrow. In what way doesn't it? An error message? No record inserted?

Personally I would use "INSERT INTO table blah blah" rather than the recordset approach, and then do db.Execute on that SQL statement.
Australia #3
The error is "type mismatch" *boggle*
Have fun :P
Australia Forum Administrator #4
After quite a bit of mucking around, I did not get the "addnew" approach to work. However the direct execution of SQL seems to work fine, so I suggest using this, rather than killing yourself. :)


dim db
Set db = CreateObject ("ADODB.Connection")
db.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=z:\test.mdb"

db.Execute "INSERT INTO itemtable VALUES (0, ""a"", ""b"", 5, 6);"

set db = nothing