Click here to Skip to main content
15,893,266 members

Comments by kyrons (Top 14 by date)

kyrons 14-Feb-21 3:49am View    
Here is my contructed code but not working.

Dim connstring As String
connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=studentrecords.accdb"
Dim conn As OleDbConnection = New OleDbConnection(connstring)
Dim displaystr As String = "SELECT studentfullname, dateofbirth, Round(DateDiff('m',studenprofile_records.Date_of_Birth,Date())/12,2) AS Age FROM studentprofile_records WHERE Age BETWEEN'" & CInt(txtagefrom.Text) & "' AND Age <='" & CInt(txtageto.Text) & "'"
Dim cmddisplay As New OleDbCommand(displaystr, conn)
conn.Open()
cmddisplay.ExecuteNonQuery()
conn.Close()

Me.ReportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.ZoomMode.PageWidth)
Me.ReportViewer1.RefreshReport()
kyrons 24-Jun-20 5:51am View    
This is the corrected codes:

Code:
Dim con1 As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= database1.accdb")
Dim con2 As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= database2.accdb")

con2.Open()
Dim CompQuery As String = "SELECT COUNT(*) FROM sampletable WHERE FirstName = @FirstName AND LastName = @LastName"
Dim compCommand As OleDbCommand = New OleDbCommand(CompQuery, con2)
compCommand.Parameters.AddWithValue("@FirstName", "FirstName")
compCommand.Parameters.AddWithValue("@LastName", "LastName")

If Convert.ToInt32(compCommand.ExecuteScalar()) > 0 Then
'ignoring duplicate entries msgbox("Some records fail to save because it would create duplicate
entries!")
Exit Sub
Else

'Create the data adapter with a SelectCommand using the first connection.
Dim da As New OleDb.OleDbDataAdapter("SELECT FirstName, LastName FROM table1 ", con1)
'Add the InsertCommand with the second connection.
da.InsertCommand = New OleDb.OleDbCommand("INSERT INTO sampletable (FirstName, LastName) VALUES (@FirstName, @LastName)", con2)
'Add the insert parameters.
da.InsertCommand.Parameters.Add("@FirstName", OleDb.OleDbType.VarChar, 50, "FirstName")
da.InsertCommand.Parameters.Add("@LastName", OleDb.OleDbType.VarChar, 50, "LastName")
'Keep the records in a state where they can be inserted into the destination table.
da.AcceptChangesDuringFill = False
Dim dt As New DataTable
'Get the data from the source database.
da.Fill(dt)
'Save the data to the destination database.
da.Update(dt)
MsgBox("Data Added!")

con1.Dispose()
con2.Dispose()

End If
kyrons 24-Jun-20 5:15am View    
Sorry that in this portion of code was mistaken. It was just typographical error. Sorry that it making you confused.

"But when you insert with the command

da.InsertCommand = New OleDb.OleDbCommand("INSERT INTO sampletable (Fullname, FirstName) VALUES (@FirstName, @LastName)", con2)"

Instead:

da.InsertCommand = New OleDb.OleDbCommand("INSERT INTO sampletable (FirstName, LastName) VALUES (@FirstName, @LastName)", con2)
kyrons 3-Jun-20 4:47am View    
Wow...Thank you so much Richard Deeming, my friend, it works perfectly.
kyrons 11-May-20 6:30am View    
Why if I delete "name" it proceeds adding some columns but if it still there the columns are not adding? I think the "name" is the one that is recognized that if it is existing you cannot add columns.