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.
"Runtime error '91: Object variable or With block variable not set."
Informatie van MSDN over deze error:
"First you must declare the object variable. Then you must assign a valid reference to the object variable using the Set statement."
Declaration wordt gedaan en Set statement wordt gebruikt, maar kennelijk dus op de verkeerde manier.
Weet iemand wat er dan precies fout gaat?
code waar het probleem in zit (regel 8 zit de fout in)
Hoe heb je dan je database gemaakt? (in dit stuk geen errors)
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.
"Runtime error '91: Object variable or With block variable not set."
Informatie van MSDN over deze error:
"First you must declare the object variable. Then you must assign a valid reference to the object variable using the Set statement."
Declaration wordt gedaan en Set statement wordt gebruikt, maar kennelijk dus op de verkeerde manier.
Weet iemand wat er dan precies fout gaat?
code waar het probleem in zit (regel 8 zit de fout in)
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| 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
'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 |
Hoe heb je dan je database gemaakt? (in dit stuk geen errors)
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
| 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 |