Sub Update_List()
Dim db As DAO.Database, rst As DAO.Recordset
Dim path As String
Dim wb As Workbook
Dim w1 As Worksheet
Dim qry As String, lst As String
Dim err As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Change your path
path = "C:\Folder\Your.mdb"
Set db = OpenDatabase(path)
Set wb = ThisWorkbook
Set w1 = wb.Worksheets("Sheet1")
'Change field name and Table name
qry = "SELECT DISTINCT FieldWithData FROM YourTable;"
lst = ""
Set rst = db.OpenRecordset(qry)
With rst
If .EOF = True And .BOF = True Then
.MoveFirst
Do Until .EOF
lst = lst & .Fields("FieldWithData").Value & ","
.MoveNext
Loop
Else
'show error if dataset is empty
err = MsgBox("No data to import", vbOKOnly, "No data to import")
End If
End With
rst.Close
'Change the range where you want your Validation list
With w1.Range("A1").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & x
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = "Please select from dropdown list."
.ShowInput = True
.ShowError = True
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'this will run your code every time value of validation field is changed
'Change "$A$1" to any other range
If Target.Address = "$A$1" Then
'Do something
End If
End Sub
|