hello

can u help me to store data from vb.net to access...right now i get the data from com port. and i want the data to be save in the access...below is the sample code...

Public Class frmSerialPortExample
    '----------------------------------------------------------------------------------------------------------
    'Purpose:  Allow for data recieve event to update text box on different thread
    '----------------------------------------------------------------------------------------------------------
    Public Delegate Sub myDelegate()

    '----------------------------------------------------------------------------------------------------------
    'Purpose:  Close and exit
    '----------------------------------------------------------------------------------------------------------
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Close()
    End Sub

    '----------------------------------------------------------------------------------------------------------
    'Purpose:  Load Form
    '----------------------------------------------------------------------------------------------------------
    Private Sub frmSerialPortExample_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
            cboCommPorts.Items.Add(My.Computer.Ports.SerialPortNames(i))
        Next
        cmdDisconnect.Enabled = False
    End Sub

    '----------------------------------------------------------------------------------------------------------
    'Purpose:  Connect to comm port
    '----------------------------------------------------------------------------------------------------------
    Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
        If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If
        Try
            With SerialPort1
                .PortName = cboCommPorts.Text
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
            End With
            SerialPort1.Open()

            lblMessage.Text = SerialPort1.PortName & " connected"
            cmdDisconnect.Enabled = True
            cmdConnect.Enabled = False
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    '----------------------------------------------------------------------------------------------------------
    'Purpose:  Disconnect from comm port
    '----------------------------------------------------------------------------------------------------------
    Private Sub cmdDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisconnect.Click
        Try
            SerialPort1.Close()
            lblMessage.Text = SerialPort1.PortName & " disconnected"
            cmdDisconnect.Enabled = False
            cmdConnect.Enabled = True
        Catch ex As Exception

        End Try
    End Sub

    '----------------------------------------------------------------------------------------------------------
    'Purpose:  Send message to selected comm port
    '----------------------------------------------------------------------------------------------------------
    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        Try
            SerialPort1.Write(txtTextToSend.Text & vbCrLf)
            With txtDataRecieved
                .SelectionColor = Color.Black
                .AppendText(txtTextToSend.Text & vbCrLf)
                .ScrollToCaret()
            End With
            txtTextToSend.Text = String.Empty
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    '----------------------------------------------------------------------------------------------------------
    'Purpose:  Update text box with data recieved from comm port
    '----------------------------------------------------------------------------------------------------------
    Public Sub updateTextBox()
        With txtDataRecieved
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .SelectionColor = Color.Red
            .AppendText(SerialPort1.ReadExisting)
            .ScrollToCaret()
        End With
    End Sub

    '----------------------------------------------------------------------------------------------------------
    'Purpose:  Receive data
    '----------------------------------------------------------------------------------------------------------
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        txtDataRecieved.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})  ', New Object()
    End Sub

End Class

the string of data which i get from com port are like this ' $HUB0, 30.9C, 3.5, 000, N#'

and i don't want to store the data into one column...but separate it into 5 column per data...

can u help me please..because i need to submit my task to lecturer next week..

i already able to load data into access...below is code which i have add...

Public Sub updateTextBox()

      Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=C:\tab\db1.mdb")
        Dim sql2 As String = String.Empty
        con.Open()

        sql2 = "INSERT INTO Table1 (Data) VALUES ('" & txtDataRecieved.Text & "');"
        Dim cmd As New OleDbCommand(sql2, con)
        cmd.ExecuteNonQuery()
        con.Close()

        With txtDataRecieved
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .SelectionColor = Color.Red
            .AppendText(SerialPort1.ReadExisting)
            .ScrollToCaret()

        End With
End Sub

but still have problem where data insert in table into one row only..all string of data from the comp port jumble up into one row..i want it to be one string per one row...
i really hope anyone here can help me..I already put my afford to did this..please...

Private Sub send()
Try
SerialPort1.Write(txtTextToSend.Text)
With RichTextBox1
.SelectionColor = Color.Black
.AppendText(vbCrLf & txtTextToSend.Text)
.ScrollToCaret()
End With
txtTextToSend.Text = String.Empty
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

'Purpose: Update text box with data recieved from comm port
Public Sub updateTextBox()

With RichTextBox1
.Font = New Font("calibri", 12.0!, FontStyle.Regular)
.SelectionColor = Color.Red
.AppendText(vbCrLf & SerialPort1.ReadExisting)
.ScrollToCaret()
End With
End Sub

* i jus edit the vbCrLf position. vbCrLf is for enter purpose.*
i m very new in VB.net.so..i hope i m not direct u to a wrong path =p

grace

tq yun_e for your answer..

i have try ur solution but still the same..
btw..
i have solve the problem to store data row by row..below is the coding...

Public Sub updateTextBox()
        'System.Threading.Thread.Sleep(3000)
        With txtDataRecieved
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .SelectionColor = Color.Red
            .AppendText(SerialPort1.ReadExisting)
            .ScrollToCaret()
        End With
    End Sub
    Public Sub updateDatabase()

        With txtDataRecieved
            'System.Threading.Thread.Sleep(3000)
            'If Not txtDataRecieved.TextLength = 0 Then
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=C:\tab\db1.mdb")
            Dim sql2 As String = String.Empty
            con.Open()
            sql2 = "INSERT INTO Table1 (HUB) VALUES ('" & txtDataRecieved.Text & "');"
            Dim cmd As New OleDbCommand(sql2, con)
            System.Threading.Thread.Sleep(5000)
            .Clear()
            cmd.ExecuteNonQuery()
            con.Close()
            ' End If

            '.Clear()
        End With


    End Sub

    '----------------------------------------------------------------------------------------------------------
    'Purpose:  Receive data
    '----------------------------------------------------------------------------------------------------------
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        txtDataRecieved.Invoke(New myDelegate(AddressOf updateDatabase), New Object() {})  ', New Object()

        txtDataRecieved.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})  ', New Object()
    End Sub
End Class

but right now i want to figure out how can i split string of data in my table into separate column..right now i only have one column in my table which contain the data..the data in the column is like this '$0001, 85.8F,3.0,069,N#...anyone can help me...

This article has been dead for over six months. Start a new discussion instead.