I need to link an access database to my visual vasic express 2008 class i have made (as been set an object orientated project) but am struggling to link the database and get it to show the information from the database through the class to the product form.
I'll attach what code i have done (i have 2 different lots as one i tried to follow another one on the internet, but just ended up confusing myself (as im a fairly basic amateur))
The one i have tried myself looks as follows:
Public Class clsItems Dim cn As New ADODB.Connection Dim RS As New ADODB.Recordset Private xProduct_id As Integer Private xName As String Private xDescription As String Private xPrice As Integer Private xCategory As String Private xMake As String Public Sub SetProduct_id(ByVal xProduct As Integer) xProduct_id = xProduct End Sub Public Function GetProduct_id() As Integer GetProduct_id = xProduct_id End Function Public Sub SetName(ByVal xNew As String) xName = xNew End Sub Public Function GetName() As String GetName = xName End Function Public Sub SetDescription(ByVal xDesc As String) xDescription = xDesc End Sub Public Function GetDescription() As String GetDescription = xDescription End Function Public Sub SetPrice(ByVal xPri As Integer) xPrice = xPri End Sub Public Function Getprice() As Integer Getprice = xPrice End Function Public Sub SetCategory(ByVal xCat As String) xCategory = xCat End Sub Public Function Getcategory() As String Getcategory = xCategory End Function Public Sub SetMake(ByVal xMa As String) xMake = xMa End Sub Public Function Getmake() As String Getmake = xMake End Function Public Function FetchData() As Object ' GetProduct_id = RS.Fields("Product_id").Value End Function End Class
For some reason we were shown to do the set and get methods like that, but don't quite see what they do.. I get to the point with the fetchdata and just end up with an error 'Expression is a value and therefore cannot be a target of an assignment'
The other attempt i have made (by following an example):
Public Class Class1 Dim cn As New ADODB.Connection Dim RS As New ADODB.Recordset Private xName As String Private xDescription As String Private xPrice As Integer Private xCategory As String Private xMake As String Public Property Name() As String Get Name = xName End Get Set(ByVal Value As String) xName = Value End Set End Property Public Property Description() As String Get Description = xDescription End Get Set(ByVal value As String) xDescription = value End Set End Property Public Property Price() As String Get Price = xPrice End Get Set(ByVal value As String) xPrice = value End Set End Property Public Property Category() As String Get Category = xCategory End Get Set(ByVal value As String) xCategory = value End Set End Property Public Property Make() As String Get Make = xMake End Get Set(ByVal value As String) xMake = value End Set End Property Public Function FetchData() As Object Name = RS.Fields("Name").Value Description = RS.Fields("Description").Value Price = RS.Fields("Price").Value Category = RS.Fields("Category").Value Make = RS.Fields("Make").Value End Function Private Sub Class_Initialise() cn = New ADODB.Connection RS = New ADODB.Recordset cn.Provider = "Microsoft.Jet.OLEDB.3.51" cn.ConnectionString = My.Application.Info.DirectoryPath & "\products.mdb" cn.Open() RS.Open("select * from [tbl_products]", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockBatchOptimistic) End Sub Public Sub New() MyBase.New() Class_Initialise() End Sub Private Sub Class_Terminate() cn.Close() RS.Close() End Sub Protected Overrides Sub Finalize() Class_Terminate() MyBase.Finalize() End Sub This one seems to work slightly more succesfully with the program only saying 'use sub finalize to free resources' (this is what i thought the bit below class terminate did)?? The form im trying to link it to so far comnprises of; Public x As New Class1 Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) x.FetchData() txtname.Text = x.Name txtdes.Text = x.Description txtprice.Text = x.Price txtprod.Text = x.Name txtmake.Text = x.Make
Where i thought making the text boxes on the form equal to the coinciding bits of the class would work..
If anybody can see where the problems occur or ways in which to solve them then that would be excellent and much appreciated as currently im getting very stressed trying to work this program out
p.s. there is probably a better way to show the code on here, and i understand my codeing is probably very messy but as i say im an amateur, any advice is thankfully received