Er moet een relationele database in visual basic code gemaakt worden. (opdracht voor school, werktuigbouwkunde)
De bedoeling is om een record toe te voegen, maar dit lukt niet.
Hierbij ontstaat het probleem dat de recordset myRec niet geladen wordt.
myRec is uiteraard wel gedefinieerd als Recordset.
De Sub AddProductList wordt wel gewoon gestart.
In de code staat (bijna helemaal onderaan) in "Sub AddProductList()"de error aangegeven.
Weet iemand hoe dit komt en hoe dit opgelost kan worden??
Code:
Sub Form_Load()
'define variables
Dim dbFile As Database, dbWS As Workspace
Dim ProductList As TableDef, GereedschapList As TableDef, OrderList As TableDef
Dim plFields(1 To 4) As Field, glFields(1 To 4) As Field, olFields(1 To 3) As Field
Dim plIndex As Index, glIndex As Index, olIndex As Index
Dim relate As Relation
Dim myRec As Recordset
Dim count As Integer
'create the Database Or open it when it exists
Set dbWS = DBEngine.Workspaces(0)
If Dir("C:\Product.mdb") <> "" Then
Open ("C:\Product.mdb") For Random As #1
Else
Set dbFile = dbWS.CreateDatabase("C:\Product.mdb", dbLangGeneral)
'create the Productlist Tabel
Set ProductList = dbFile.CreateTableDef("ProductList")
Set plFields(1) = ProductList.CreateField("ProductCode", dbText, 10)
Set plFields(2) = ProductList.CreateField("Gereedschap", dbText, 20)
Set plFields(3) = ProductList.CreateField("Voorraad", dbText, 10)
Set plFields(4) = ProductList.CreateField("Verkoop", dbText, 10)
'Add the new fields to the Productlist Table
For teller = 1 To 4
ProductList.Fields.Append plFields(teller)
Next teller
'specify a primary key for the ProductList Table
Set plIndex = ProductList.CreateIndex("ProductCode")
plIndex.Primary = True
plIndex.Unique = False
plIndex.Required = True
Set plFields(4) = plIndex.CreateField("ProductCode")
'Add Primary key field to the field list of the index
plIndex.Fields.Append plFields(4)
'Add this index to the index list of the table
ProductList.Indexes.Append plIndex
'Add the table to the database
dbFile.TableDefs.Append ProductList
'Create the GereedschapList Table
Set GereedschapList = dbFile.CreateTableDef("GereedschapList")
Set glFields(1) = GereedschapList.CreateField("GereedschapCode", dbText, 20)
Set glFields(2) = GereedschapList.CreateField("Levertijd", dbText, 50)
Set glFields(3) = GereedschapList.CreateField("Voorraad", dbText, 20)
Set glFields(4) = GereedschapList.CreateField("Gebruiksduur", dbText, 25)
'Add the new fields to the GereedschapList Table
For teller = 1 To 4
GereedschapList.Fields.Append glFields(teller)
Next teller
'specify a primary key for the GereedschapList Table
Set glIndex = GereedschapList.CreateIndex("GereedschapCode")
glIndex.Primary = True
glIndex.Unique = True
glIndex.Required = True
Set glFields(1) = glIndex.CreateField("GereedschapCode")
'Add Primary key field to the field list of the index
glIndex.Fields.Append glFields(1)
'Add this index to the index list of the table
GereedschapList.Indexes.Append glIndex
'Add the table to the database
dbFile.TableDefs.Append GereedschapList
'create the Orderlist Tabel
Set OrderList = dbFile.CreateTableDef("OrderList")
Set olFields(1) = OrderList.CreateField("OrderNummer", dbText, 10)
Set olFields(2) = OrderList.CreateField("KlantNummer", dbText, 20)
Set olFields(3) = OrderList.CreateField("ProductCode", dbText, 10)
'Add the new fields to the Orderlist Table
For teller = 1 To 3
OrderList.Fields.Append olFields(teller)
Next teller
'specify a primary key for the OrderList Table
Set olIndex = OrderList.CreateIndex("OrderNummer")
olIndex.Primary = True
olIndex.Unique = False
olIndex.Required = True
Set olFields(3) = olIndex.CreateField("OrderNummer")
'Add Primary key field to the field list of the index
olIndex.Fields.Append olFields(3)
'Add this index to the index list of the table
OrderList.Indexes.Append olIndex
'Add the table to the database
dbFile.TableDefs.Append OrderList
'Set up the relation between the tables
Set relate = dbFile.CreateRelation("foreign", "GereedschapList", "ProductList")
relate.Attributes = 0
'Mark the foreign key in ProductList
''''''''''''''''''' plFields(1).ForeignName = "ProductCode"
'Mark the primary field in GereedschapList
Set glFields(1) = relate.CreateField("GereedschapCode")
'Add the field to the field list of the relation
'''''''''''''''''relate.Fields.Append plFields(1)
'Add the relation to the database
'''''''''''''dbFile.Relations.Append relate
'Open a Recordset referring to ProductList
'Set myRec = ProductList
Set myRec = ProductList.OpenRecordset
'Create new records in ProductList
myRec.AddNew
myRec("ProductCode") = "txtProductCode.Text"
myRec("Gereedschap") = "txtGereedschap.Text"
myRec("Voorraad") = "txtVoorraad.Text"
myRec("Verkoop") = "txtVerkoop.Text"
myRec.Update
'Close the Recordset
myRec.Close
'Close the Database
dbFile.Close
End If
End Sub
Sub AddProductList()
Dim dbFile As Database, dbWS As Workspace
Dim ProductList As TableDef
Dim myRec As Recordset
'Open a Recordset referring to ProductList
Set myRec = ProductList.OpenRecordset
##Error onstaat in bovenstaande regel.
##De error = Runtime error '91: Object variable or With block variable not set.
##De database is gevuld met data, maar de recordset wordt dus niet in myRec geladen.
'Create new records in ProductList
myRec.AddNew
myRec("ProductCode") = "frmAddProductList.txtProductCode.Text"
myRec("Gereedschap") = "frmAddProductList.txtGereedschap.Text"
myRec("Voorraad") = "frmAddProductList.txtVoorraad.Text"
myRec("Verkoop") = "frmAddProductList.txtVerkoop.Text"
myRec.Update
'Close the Recordset
myRec.Close
End Sub
De bedoeling is om een record toe te voegen, maar dit lukt niet.
Hierbij ontstaat het probleem dat de recordset myRec niet geladen wordt.
myRec is uiteraard wel gedefinieerd als Recordset.
De Sub AddProductList wordt wel gewoon gestart.
In de code staat (bijna helemaal onderaan) in "Sub AddProductList()"de error aangegeven.
Weet iemand hoe dit komt en hoe dit opgelost kan worden??
Code:
Sub Form_Load()
'define variables
Dim dbFile As Database, dbWS As Workspace
Dim ProductList As TableDef, GereedschapList As TableDef, OrderList As TableDef
Dim plFields(1 To 4) As Field, glFields(1 To 4) As Field, olFields(1 To 3) As Field
Dim plIndex As Index, glIndex As Index, olIndex As Index
Dim relate As Relation
Dim myRec As Recordset
Dim count As Integer
'create the Database Or open it when it exists
Set dbWS = DBEngine.Workspaces(0)
If Dir("C:\Product.mdb") <> "" Then
Open ("C:\Product.mdb") For Random As #1
Else
Set dbFile = dbWS.CreateDatabase("C:\Product.mdb", dbLangGeneral)
'create the Productlist Tabel
Set ProductList = dbFile.CreateTableDef("ProductList")
Set plFields(1) = ProductList.CreateField("ProductCode", dbText, 10)
Set plFields(2) = ProductList.CreateField("Gereedschap", dbText, 20)
Set plFields(3) = ProductList.CreateField("Voorraad", dbText, 10)
Set plFields(4) = ProductList.CreateField("Verkoop", dbText, 10)
'Add the new fields to the Productlist Table
For teller = 1 To 4
ProductList.Fields.Append plFields(teller)
Next teller
'specify a primary key for the ProductList Table
Set plIndex = ProductList.CreateIndex("ProductCode")
plIndex.Primary = True
plIndex.Unique = False
plIndex.Required = True
Set plFields(4) = plIndex.CreateField("ProductCode")
'Add Primary key field to the field list of the index
plIndex.Fields.Append plFields(4)
'Add this index to the index list of the table
ProductList.Indexes.Append plIndex
'Add the table to the database
dbFile.TableDefs.Append ProductList
'Create the GereedschapList Table
Set GereedschapList = dbFile.CreateTableDef("GereedschapList")
Set glFields(1) = GereedschapList.CreateField("GereedschapCode", dbText, 20)
Set glFields(2) = GereedschapList.CreateField("Levertijd", dbText, 50)
Set glFields(3) = GereedschapList.CreateField("Voorraad", dbText, 20)
Set glFields(4) = GereedschapList.CreateField("Gebruiksduur", dbText, 25)
'Add the new fields to the GereedschapList Table
For teller = 1 To 4
GereedschapList.Fields.Append glFields(teller)
Next teller
'specify a primary key for the GereedschapList Table
Set glIndex = GereedschapList.CreateIndex("GereedschapCode")
glIndex.Primary = True
glIndex.Unique = True
glIndex.Required = True
Set glFields(1) = glIndex.CreateField("GereedschapCode")
'Add Primary key field to the field list of the index
glIndex.Fields.Append glFields(1)
'Add this index to the index list of the table
GereedschapList.Indexes.Append glIndex
'Add the table to the database
dbFile.TableDefs.Append GereedschapList
'create the Orderlist Tabel
Set OrderList = dbFile.CreateTableDef("OrderList")
Set olFields(1) = OrderList.CreateField("OrderNummer", dbText, 10)
Set olFields(2) = OrderList.CreateField("KlantNummer", dbText, 20)
Set olFields(3) = OrderList.CreateField("ProductCode", dbText, 10)
'Add the new fields to the Orderlist Table
For teller = 1 To 3
OrderList.Fields.Append olFields(teller)
Next teller
'specify a primary key for the OrderList Table
Set olIndex = OrderList.CreateIndex("OrderNummer")
olIndex.Primary = True
olIndex.Unique = False
olIndex.Required = True
Set olFields(3) = olIndex.CreateField("OrderNummer")
'Add Primary key field to the field list of the index
olIndex.Fields.Append olFields(3)
'Add this index to the index list of the table
OrderList.Indexes.Append olIndex
'Add the table to the database
dbFile.TableDefs.Append OrderList
'Set up the relation between the tables
Set relate = dbFile.CreateRelation("foreign", "GereedschapList", "ProductList")
relate.Attributes = 0
'Mark the foreign key in ProductList
''''''''''''''''''' plFields(1).ForeignName = "ProductCode"
'Mark the primary field in GereedschapList
Set glFields(1) = relate.CreateField("GereedschapCode")
'Add the field to the field list of the relation
'''''''''''''''''relate.Fields.Append plFields(1)
'Add the relation to the database
'''''''''''''dbFile.Relations.Append relate
'Open a Recordset referring to ProductList
'Set myRec = ProductList
Set myRec = ProductList.OpenRecordset
'Create new records in ProductList
myRec.AddNew
myRec("ProductCode") = "txtProductCode.Text"
myRec("Gereedschap") = "txtGereedschap.Text"
myRec("Voorraad") = "txtVoorraad.Text"
myRec("Verkoop") = "txtVerkoop.Text"
myRec.Update
'Close the Recordset
myRec.Close
'Close the Database
dbFile.Close
End If
End Sub
Sub AddProductList()
Dim dbFile As Database, dbWS As Workspace
Dim ProductList As TableDef
Dim myRec As Recordset
'Open a Recordset referring to ProductList
Set myRec = ProductList.OpenRecordset
##Error onstaat in bovenstaande regel.
##De error = Runtime error '91: Object variable or With block variable not set.
##De database is gevuld met data, maar de recordset wordt dus niet in myRec geladen.
'Create new records in ProductList
myRec.AddNew
myRec("ProductCode") = "frmAddProductList.txtProductCode.Text"
myRec("Gereedschap") = "frmAddProductList.txtGereedschap.Text"
myRec("Voorraad") = "frmAddProductList.txtVoorraad.Text"
myRec("Verkoop") = "frmAddProductList.txtVerkoop.Text"
myRec.Update
'Close the Recordset
myRec.Close
End Sub