create an access quey to do the following

samipm

New Member
Joined
Mar 27, 2012
Messages
35
Hi guys

The following code is run when a click button is clicked on a form


Const ExcelFile As String = "Employees.xls"
Dim ExcApp As Object
Dim WrkBk As Object
Dim WrkSht As Object
Dim CurrRow As Integer
Dim GNam As String
Dim LNam As String
Dim SexFld As String
Dim SqlCmd As String

On Error Resume Next

Set ExcApp = GetObject(, "Excel.Application")

If Err.Number <> 0 Then
'Excel wasn't running, start it from code
Set ExcApp = CreateObject("Excel.Application")
End If

On Error GoTo 0

' Opens Excel and makes it Visible
Set WrkBk = ExcApp.Workbooks.Open(CurrentProject.Path & "\" & ExcelFile, ReadOnly:=True)
Set WrkSht = WrkBk.Sheets("Customer")
CurrRow = 2

Do Until IsEmpty(WrkSht.Cells(CurrRow, "B").Value)
With WrkSht
LNam = .Cells(CurrRow, "B").Value
GNam = .Cells(CurrRow, "A").Value
SexFld = .Cells(CurrRow, "C").Value
If IsNull(DLookup("Employees.FirstName", "Employees", "Employees.LastName = '" & LNam & "' And Employees.FirstName = '" & GNam & "'")) Then
SqlCmd = "INSERT INTO Employees " & _
"(FirstName, LastName, Sex) " & _
"VALUES " & _
"('" & GNam & "', '" & LNam & "', '" & SexFld & "')"
Else
SqlCmd = "UPDATE Employees " & _
"SET Employees.Sex = '" & SexFld & "' " & _
"WHERE Employees.LastName = '" & LNam & "' " & _
"AND Employees.FirstName = '" & GNam & "'"
End If
CurrentDb.Execute SqlCmd, dbFailOnError
End With
CurrRow = CurrRow + 1
Loop

WrkBk.Close SaveChanges:=False

Can I convert the SqlCmd commands to an access queries?

God Bless

Sami
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Why struggle with the code when you can link excel data to Access (from Access) and create a Query from the linked table?
 
Upvote 0
First, you must ensure that the data you are trying to bring into Access are created conform to database rules in Excel. Search the topic DATABASE in excel to get documents listed about this subject and go through them.

There is a menu option in Access to Link external data sources including Excel. It is difficult to explain all the details here.
 
Upvote 0

Forum statistics

Threads
1,215,202
Messages
6,123,625
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top