VB.NET--MS Acess DataBase Problem

Started by
-1 comments, last by drummer1549 18 years, 9 months ago
Hey, im just getting into database programming with ado.net an vb.net. I created a fairly simple application where i created a ms acces table with fields such as name,b-day,age,sex,etc... I want to load that into my vb application and display it in a listbox, which i can do. But i also want to be able to enter the info into textboxes on my form then click a button an have ti enter the info in the textboxes into the table and then refresh the listbox with the new info added. I think i have the basic code for this down but one line keeps giving me an error and i cant get seem to fix the problem. So here is my code, ive capitalized and put asterisks(sp?) around the line that the error\debug message is pointing me to. Any help would be greatly appreciated. Also, in case u didnt quite understand what i want to be able to do, here is a link to a picture of my form, maybe this will help a little. http://img293.imageshack.us/img293/7755/screenshot3au.jpg Here is the code...

'
' Created by SharpDevelop.
' User: Mueller
' Date: 7/10/2005
' Time: 12:39 PM
' 
' To change this template use Tools | Options | Coding | Edit Standard Headers.
'
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.Data
imports Microsoft.VisualBasic
Namespace IEtest
	
	Public Class MainForm
		Inherits System.Windows.Forms.Form
		 Dim myOleDbDataAdapter As OleDbDataAdapter
    Dim myDataSet As DataSet = New DataSet()
    Dim myDataTable As DataTable = New DataTable()
    Dim myOleDbConnection As OleDbConnection
    Dim myCommand As OleDbCommandBuilder
    Dim artKeySave As Integer
		Dim I As Integer = 0
		Dim note(999) As String
		Dim id As Integer
		Dim transfer As String
		Dim count As Integer
		Dim x As Integer
		Dim letter As String
		Dim picture(999) As bitmap
		dim viewpic as boolean
		Private radioButton2 As System.Windows.Forms.RadioButton
		Private radioButton1 As System.Windows.Forms.RadioButton
		Private notes As System.Windows.Forms.TextBox
		Private button3 As System.Windows.Forms.Button
		Private button2 As System.Windows.Forms.Button
		Private button1 As System.Windows.Forms.Button
		Private textBox8 As System.Windows.Forms.TextBox
		Private textBox5 As System.Windows.Forms.TextBox
		Private textBox4 As System.Windows.Forms.TextBox
		Private textBox6 As System.Windows.Forms.TextBox
		Private textBox1 As System.Windows.Forms.TextBox
		Private textBox2 As System.Windows.Forms.TextBox
		Private checkBox1 As System.Windows.Forms.CheckBox
		Private label3 As System.Windows.Forms.Label
		Private label2 As System.Windows.Forms.Label
		Private label1 As System.Windows.Forms.Label
		Private label7 As System.Windows.Forms.Label
		Private label6 As System.Windows.Forms.Label
		Private label5 As System.Windows.Forms.Label
		Private label4 As System.Windows.Forms.Label
		Private listbox1 As System.Windows.Forms.ListBox
		Private textBox3 As System.Windows.Forms.TextBox
		
		Public Shared Sub Main
			Dim fMainForm As New MainForm
			fMainForm.ShowDialog()
		End Sub
		
		Public Sub New()
			MyBase.New
			'
			' The Me.InitializeComponent call is required for Windows Forms designer support.
			'
			Me.InitializeComponent
			'
			' TODO : Add constructor code after InitializeComponents
			'
		End Sub
		
		#Region " Windows Forms Designer generated code "
		' This method is required for Windows Forms designer support.
		' Do not change the method contents inside the source code editor. The Forms designer might
		' not be able to load this method if it was changed manually.
		Private Sub InitializeComponent()
			Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(MainForm))
			Me.textBox3 = New System.Windows.Forms.TextBox
			Me.listbox1 = New System.Windows.Forms.ListBox
			Me.label4 = New System.Windows.Forms.Label
			Me.label5 = New System.Windows.Forms.Label
			Me.label6 = New System.Windows.Forms.Label
			Me.label7 = New System.Windows.Forms.Label
			Me.label1 = New System.Windows.Forms.Label
			Me.label2 = New System.Windows.Forms.Label
			Me.label3 = New System.Windows.Forms.Label
			Me.checkBox1 = New System.Windows.Forms.CheckBox
			Me.textBox2 = New System.Windows.Forms.TextBox
			Me.textBox1 = New System.Windows.Forms.TextBox
			Me.textBox6 = New System.Windows.Forms.TextBox
			Me.textBox4 = New System.Windows.Forms.TextBox
			Me.textBox5 = New System.Windows.Forms.TextBox
			Me.textBox8 = New System.Windows.Forms.TextBox
			Me.button1 = New System.Windows.Forms.Button
			Me.button2 = New System.Windows.Forms.Button
			Me.button3 = New System.Windows.Forms.Button
			Me.notes = New System.Windows.Forms.TextBox
			Me.radioButton1 = New System.Windows.Forms.RadioButton
			Me.radioButton2 = New System.Windows.Forms.RadioButton
			Me.SuspendLayout
			'
			'textBox3
			'
			Me.textBox3.Location = New System.Drawing.Point(264, 16)
			Me.textBox3.Name = "textBox3"
			Me.textBox3.Size = New System.Drawing.Size(24, 21)
			Me.textBox3.TabIndex = 4
			Me.textBox3.Text = ""
			'
			'listbox1
			'
			Me.listbox1.BorderStyle = System.Windows.Forms.BorderStyle.None
			Me.listbox1.ColumnWidth = 25
			Me.listbox1.HorizontalScrollbar = true
			Me.listbox1.Location = New System.Drawing.Point(8, 112)
			Me.listbox1.Name = "listbox1"
			Me.listbox1.Size = New System.Drawing.Size(680, 312)
			Me.listbox1.TabIndex = 0
			'
			'label4
			'
			Me.label4.Location = New System.Drawing.Point(312, 40)
			Me.label4.Name = "label4"
			Me.label4.Size = New System.Drawing.Size(104, 16)
			Me.label4.TabIndex = 13
			Me.label4.Text = "Birthday"
			'
			'label5
			'
			Me.label5.Location = New System.Drawing.Point(24, 88)
			Me.label5.Name = "label5"
			Me.label5.Size = New System.Drawing.Size(104, 16)
			Me.label5.TabIndex = 14
			Me.label5.Text = "Date Started"
			'
			'label6
			'
			Me.label6.Location = New System.Drawing.Point(344, 88)
			Me.label6.Name = "label6"
			Me.label6.Size = New System.Drawing.Size(112, 16)
			Me.label6.TabIndex = 15
			Me.label6.Text = "Picture URL"
			'
			'label7
			'
			Me.label7.Location = New System.Drawing.Point(216, 88)
			Me.label7.Name = "label7"
			Me.label7.TabIndex = 21
			Me.label7.Text = "Phone Number"
			'
			'label1
			'
			Me.label1.Location = New System.Drawing.Point(32, 40)
			Me.label1.Name = "label1"
			Me.label1.Size = New System.Drawing.Size(104, 16)
			Me.label1.TabIndex = 10
			Me.label1.Text = "Last Name"
			'
			'label2
			'
			Me.label2.Location = New System.Drawing.Point(160, 40)
			Me.label2.Name = "label2"
			Me.label2.Size = New System.Drawing.Size(104, 16)
			Me.label2.TabIndex = 11
			Me.label2.Text = "First Name"
			'
			'label3
			'
			Me.label3.Location = New System.Drawing.Point(264, 40)
			Me.label3.Name = "label3"
			Me.label3.Size = New System.Drawing.Size(32, 16)
			Me.label3.TabIndex = 12
			Me.label3.Text = "Age"
			'
			'checkBox1
			'
			Me.checkBox1.Location = New System.Drawing.Point(448, 56)
			Me.checkBox1.Name = "checkBox1"
			Me.checkBox1.Size = New System.Drawing.Size(96, 16)
			Me.checkBox1.TabIndex = 18
			Me.checkBox1.Text = "View Notes"
			'
			'textBox2
			'
			Me.textBox2.Location = New System.Drawing.Point(144, 16)
			Me.textBox2.Name = "textBox2"
			Me.textBox2.Size = New System.Drawing.Size(104, 21)
			Me.textBox2.TabIndex = 3
			Me.textBox2.Text = ""
			'
			'textBox1
			'
			Me.textBox1.Location = New System.Drawing.Point(8, 16)
			Me.textBox1.Name = "textBox1"
			Me.textBox1.Size = New System.Drawing.Size(128, 21)
			Me.textBox1.TabIndex = 2
			Me.textBox1.Text = ""
			'
			'textBox6
			'
			Me.textBox6.Location = New System.Drawing.Point(320, 64)
			Me.textBox6.Name = "textBox6"
			Me.textBox6.Size = New System.Drawing.Size(112, 21)
			Me.textBox6.TabIndex = 7
			Me.textBox6.Text = ""
			'
			'textBox4
			'
			Me.textBox4.Location = New System.Drawing.Point(312, 16)
			Me.textBox4.Name = "textBox4"
			Me.textBox4.TabIndex = 5
			Me.textBox4.Text = ""
			'
			'textBox5
			'
			Me.textBox5.Location = New System.Drawing.Point(8, 64)
			Me.textBox5.Name = "textBox5"
			Me.textBox5.Size = New System.Drawing.Size(104, 21)
			Me.textBox5.TabIndex = 6
			Me.textBox5.Text = ""
			'
			'textBox8
			'
			Me.textBox8.Location = New System.Drawing.Point(192, 64)
			Me.textBox8.Name = "textBox8"
			Me.textBox8.Size = New System.Drawing.Size(120, 21)
			Me.textBox8.TabIndex = 20
			Me.textBox8.Text = ""
			'
			'button1
			'
			Me.button1.Location = New System.Drawing.Point(448, 16)
			Me.button1.Name = "button1"
			Me.button1.TabIndex = 1
			Me.button1.Text = "Add"
			AddHandler Me.button1.Click, AddressOf Me.Button1Click
			'
			'button2
			'
			Me.button2.Location = New System.Drawing.Point(696, 120)
			Me.button2.Name = "button2"
			Me.button2.Size = New System.Drawing.Size(56, 24)
			Me.button2.TabIndex = 16
			Me.button2.Text = "Remove"
			'
			'button3
			'
			Me.button3.Location = New System.Drawing.Point(696, 160)
			Me.button3.Name = "button3"
			Me.button3.Size = New System.Drawing.Size(56, 23)
			Me.button3.TabIndex = 17
			Me.button3.Text = "Notes"
			'
			'notes
			'
			Me.notes.Location = New System.Drawing.Point(168, 144)
			Me.notes.Multiline = true
			Me.notes.Name = "notes"
			Me.notes.Size = New System.Drawing.Size(336, 216)
			Me.notes.TabIndex = 19
			Me.notes.Text = ""
			Me.notes.Visible = false
			'
			'radioButton1
			'
			Me.radioButton1.Location = New System.Drawing.Point(128, 56)
			Me.radioButton1.Name = "radioButton1"
			Me.radioButton1.TabIndex = 8
			Me.radioButton1.Text = "Male"
			'
			'radioButton2
			'
			Me.radioButton2.Location = New System.Drawing.Point(128, 72)
			Me.radioButton2.Name = "radioButton2"
			Me.radioButton2.TabIndex = 9
			Me.radioButton2.Text = "Female"
			'
			'MainForm
			'
			Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
			Me.ClientSize = New System.Drawing.Size(760, 494)
			Me.Controls.Add(Me.label7)
			Me.Controls.Add(Me.textBox8)
			Me.Controls.Add(Me.notes)
			Me.Controls.Add(Me.checkBox1)
			Me.Controls.Add(Me.button3)
			Me.Controls.Add(Me.button2)
			Me.Controls.Add(Me.label6)
			Me.Controls.Add(Me.label5)
			Me.Controls.Add(Me.label4)
			Me.Controls.Add(Me.label3)
			Me.Controls.Add(Me.label2)
			Me.Controls.Add(Me.label1)
			Me.Controls.Add(Me.radioButton2)
			Me.Controls.Add(Me.radioButton1)
			Me.Controls.Add(Me.textBox6)
			Me.Controls.Add(Me.textBox5)
			Me.Controls.Add(Me.textBox4)
			Me.Controls.Add(Me.textBox3)
			Me.Controls.Add(Me.textBox2)
			Me.Controls.Add(Me.textBox1)
			Me.Controls.Add(Me.button1)
			Me.Controls.Add(Me.listbox1)
			Me.Icon = CType(resources.GetObject("$this.Icon"),System.Drawing.Icon)
			Me.Name = "MainForm"
			Me.Text = "DataBase"
			AddHandler Load, AddressOf Me.MainFormLoad
			Me.ResumeLayout(false)
		End Sub
		#End Region
		
		Private Sub MainFormLoad(sender As System.Object, e As System.EventArgs)
 Dim strAboutVBConn As String = _
            "Provider=Microsoft.Jet.OLEDB.4.0;" _
            & "User ID=Admin;" _
            & "Data Source=C:\Documents and Settings\Mueller\Desktop\New Folder\New Folder\AboutVB.mdb"
        Dim ocmdAboutVBCommand As OleDbCommand
        Dim odtrAboutVBDataReader As OleDbDataReader
        
            ocmdAboutVBCommand = New OleDbCommand()
            With ocmdAboutVBCommand
                .Connection = New _
                    OleDbConnection(strAboutVBConn)
                .Connection.Open()
                .CommandText = "Select * From students"
                odtrAboutVBDataReader = .ExecuteReader()
            End With

            With listbox1
                .Items.Clear()
                .BeginUpdate()
                Do While odtrAboutVBDataReader.Read
                   .Items.Add(odtrAboutVBDataReader.Item("Last Name") & ", " & odtrAboutVBDataReader.Item("First Name") & " | Sex: " & odtrAboutVBDataReader.Item("Sex") & " | Age: " & odtrAboutVBDataReader.Item("Age") & " | Birthday: " & odtrAboutVBDataReader.Item("Birthday") & " | Date Started: " & odtrAboutVBDataReader.Item("Date Started") & " | Phone Number: " & odtrAboutVBDataReader.Item("Phone Number") & " | ID#"& odtrAboutVBDataReader.Item("ID"))
                   I += 1
                Loop
                .EndUpdate()
            End With
			myOleDbConnection = New _
               OleDbConnection( _
               "Provider=Microsoft.Jet.OLEDB.4.0;" _
               & "User ID=Admin;Data Source=C:\Documents and Settings\Mueller\Desktop\New Folder\New Folder\AboutVB.mdb")
        
            myOleDbDataAdapter = _
                New OleDbDataAdapter("select * from students", _
                myOleDbConnection)
            myOleDbDataAdapter.Fill(myDataTable)
            myCommand = New OleDbCommandBuilder(myOleDbDataAdapter)
		End Sub
		
		Private Sub Button1Click(sender As System.Object, e As System.EventArgs)
			myOleDbDataAdapter.Fill(myDataSet, "students")
            myDataTable = myDataSet.Tables("students")
            Dim newrow As DataRow = myDataTable.NewRow()
            newrow("Last Name") = textbox1.Text
            newrow("First Name") = TextBox2.Text
            
            newrow("Age") = TextBox3.Text
            newrow("Birthday") = TextBox4.Text
            newrow("Date Started") = textbox5.Text
            newrow("Phone Number") = TextBox8.Text
            myDataTable.Rows.Add(newrow)
            
     ******MYOLEDBDATAADAPTER.UPDATE(MYDATASET,"STUDENTS")*************
            myDataSet.Tables("students").AcceptChanges()
            me.Refresh()
        	'picture(id)=new bitmap(textbox6.text)
			'if radiobutton1.Checked=true then
				'listbox1.Items.add(Textbox1.text & ", " & textbox2.text & " | Male" & " |Age: " & textbox3.Text & " |Birthday: " & textbox4.Text & " |Start Date: " & textbox5.Text & "|   ID#" & id)
			'End If
			'If radiobutton2.Checked=True Then
				'listbox1.Items.add(Textbox1.text & ", " & textbox2.text & " | Female" & " |Age: " & textbox3.Text & " |Birthday: " & textbox4.Text & " |Start Date: " & textbox5.Text & "|   ID#" & id)
			'End If
			'note(id)=notes.text
			'id=id+1
			
		End Sub
		
	End Class
End Namespace





Going to Iraq without France is like going deer hunting without your accordion.

This topic is closed to new replies.

Advertisement