Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default trigger database query on cell data input

Hello

I have a question about programming Excel. I am new with excel
programming but I'm have experience with programming in other
languages.

I want to have an excel sheet populated with date from a database
query. I would like this query to be triggered by the user inputing
data in a cell and either hitting Enter or by clicking a Submit button.
For example if the user inputs in a designated cell a part number and
hits enter (or clicks a submit button) this will trigger a select * by
part bumber db query.

My question;

1. Is possible to do this in excell with VBA?
2. Are there any pointers (examples online) that I can inspire from?

Thanks
-D

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default trigger database query on cell data input

From a user Textbox you can initiate the Query using the AfterUpdate event
(if a userform) and then pass the value entered to a procedure
(myReadAccess), this procedure builds the SQL string and runs the database
query, if you do not use a userform then I would use a control button to
initiate the query taking a value entered into a cell. It is often easier
to use the macro recorder to build a template data connection and SQL using
Data-Import External Data-New Database Query from the Excel Toolbar, which
you can edit afterwards.

' in user form search string in myTextBox
Private Sub myTextBox_AfterUpdate()
Call myReadAccess(myTextBox.Value)
End Sub

' from a worksheet button - e.g. search string in A1
Private Sub CommandButton1_Click()
Call myReadAccess(Range("A1").Value)
End Sub


Sub myReadAccess(myData)
' set path and file to access database and read parameters
mdbPath = Worksheets("Data").Range("C2")
mdbFile = Worksheets("Data").Range("C3")
mdbBuffer = Worksheets("Data").Range("C7")
mdbTimeOut = Worksheets("Data").Range("C8")

'clear results range before loading new data - if required?
Range("A10:J10000").ClearContents

' build access database connection string
cConnection = "ODBC;" & _
"DBQ=" & mdbPath & mdbFile & ";" & _
"DefaultDir=" & mdbPath & ";" & _
"Driver={Microsoft Access Driver (*.mdb)};" & _
"DriverId=281;FIL=MS Access;" & _
"MaxBufferSize=" & mdbBuffer & ";" & _
"PageTimeout=" & mbdTimeOut & ";"

' compose sql
cSQL = "SELECT column1, column2, column3" & Chr(13) & "" & Chr(10) & _
"FROM `" & mdbPath & mdbFile & "`.table" & _
Chr(13) & "" & Chr(10) & "WHERE (column1='" & myData & "')" & _
Chr(13) & "" & Chr(10) & "ORDER BY table.column1, table.column2"

' go get data
With ActiveSheet.QueryTables.Add(Connection:=cConnectio n,
Destination:=Range("A10"), Sql:=cSQL)
.FieldNames = True
.RowNumbers = True
.FillAdjacentFormulas = False
'.PreserveFormatting = True
.HasAutoFormat = False
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
'.AdjustColumnWidth = False
'.RefreshPeriod = 0
'.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub


--
Cheers
Nigel



wrote in message
oups.com...
Hello

I have a question about programming Excel. I am new with excel
programming but I'm have experience with programming in other
languages.

I want to have an excel sheet populated with date from a database
query. I would like this query to be triggered by the user inputing
data in a cell and either hitting Enter or by clicking a Submit button.
For example if the user inputs in a designated cell a part number and
hits enter (or clicks a submit button) this will trigger a select * by
part bumber db query.

My question;

1. Is possible to do this in excell with VBA?
2. Are there any pointers (examples online) that I can inspire from?

Thanks
-D



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default trigger database query on cell data input

Guys,

Thank you very much for your suggestions. They are greatly appreciated.


--Dan

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tool bar: Data/Import external data/New database query Daniel Setting up and Configuration of Excel 3 February 28th 08 08:40 AM
Importing Data via Database Query Franklin Smith Excel Discussion (Misc queries) 0 October 17th 07 05:37 PM
Can you use VBA to query an Access database without importing data JonR Excel Programming 10 September 4th 07 02:00 AM
database query not showing foxpro database How I import data mangat New Users to Excel 1 June 24th 07 03:31 PM
Data Validation from a database query ? sean2000 Excel Programming 6 June 28th 05 08:52 AM


All times are GMT +1. The time now is 07:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"