Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21842

Insert data with SELECT Statement.

$
0
0
Dear SamOscarBrown,
I would like to insert data with SELECT Statement which the data is begin = 1. As the code below, I am using INSERT Statement which the data is begin = 1. But some field in the column is = 0 and the Insert Statement is not working. I want to convert INSERT Statement to SELECT …. INNER JOIN in order to update the data which the field in the column is = 0 .
Finally, I have some request:
Firstly, Insert data is beginning = 1 and if the data is exist, the data = the data + 1.
Secondly, Convert INSERT Statement to SELECT…. INNER JOIN

Code:

Private Sub cmdExcute_Click()
CommonDialog1.Filter = "(Microsoft Excel Workbook)|*.xls*"
CommonDialog1.ShowOpen
tb_upload.Text = CommonDialog1.FileName
End Sub

Private Sub cmdUpload_Click()
'On Error Resume Next
Dim ex As New Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim i As Long: i = 1
Dim con As New ADODB.Connection
Dim con1 As New ADODB.Connection

con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\TEST2.mdb;Persist Security Info=False"
con1.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\TEST2.mdb;Persist Security Info=False"
Set wb = ex.Workbooks.Open(tb_upload.Text, , True)
Set ws = wb.Worksheets(1)
Call dbConnection
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cnn

Do: i = i + 1
cmd.CommandText = "SELECT MAX([No]) as MaxField FROM SV Where MSSV = '" & ws.Range("C" & i) & "'"""
Set rs = cmd.Execute()
If rs.RecordCount > 0 And rs("MaxField") <> Empty Then
Dim sql, sql1 As String
sql1 = "UPDATE SV SET Status = 0 WHERE MSSV = '" & ws.Range("C" & i)
con.Open sql1
sql = "INSERT INTO SV([No],MSSV,MaLop,Ho,Ten,HanhKiem,[User],Status,[Date]) VALUES ('" & rs("MaxField") + 1 & "','" & ws.Range("C" & i).Value & "','" & ws.Range("A" & i).Value & "','" & ws.Range("D" & i).Value & "','" & ws.Range("E" & i).Value & "','" & ws.Range("F" & i).Value & "','" & tenUser & "','" & "-1" & "','" & NgayGioDangNhap & "')"
con.Open sql
con1.Execute "INSERT INTO Lop(MaLop,TenLop) VALUES ('" & ws.Range("A" & i).Value & "', '" & ws.Range("B" & i).Value & "')"

Else
con.Execute "INSERT INTO SV(No,MSSV,MaLop,Ho,Ten,HanhKiem,[User],Status,[Date]) VALUES (1,'" & ws.Range("C" & i).Value & "','" & ws.Range("A" & i).Value & "','" & ws.Range("D" & i).Value & "','" & ws.Range("E" & i).Value & "','" & ws.Range("F" & i).Value & "','" & tenUser & "','" & "-1" & "','" & NgayGioDangNhap & "')"
con1.Execute "INSERT INTO Lop(MaLop,TenLop) VALUES ('" & ws.Range("A" & i).Value & "', '" & ws.Range("B" & i).Value & "')"
End If
Loop Until ws.Range("A" & (i + 1)) = Empty
ex.Quit
Set ex = Nothing
con.Close
con1.Close
End Sub

Homework.zip
Attached Files

Viewing all articles
Browse latest Browse all 21842

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>