Changing VBA Code from an InputBox

Phresh10

New Member
Joined
Mar 10, 2010
Messages
29
Hi All<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
Please kindly assist with this problem. <o:p></o:p>
<o:p> </o:p>
Below is an SQL query in a VBA module that pulls data out of a database <o:p></o:p>
<o:p> </o:p>
=============================================================<o:p></o:p>
Sub CompassExtract()<o:p></o:p>
<o:p> </o:p>
sSQLDB = "PrivateDataBase"<o:p></o:p>
mstrOLEDBConnect = "Provider=SQLOLEDB.1;" & _<o:p></o:p>
"Data Source=FNS-PDBSQL-01;" & _<o:p></o:p>
"Initial Catalog=" & sSQLDB & ";" & _<o:p></o:p>
"Integrated Security=SSPI"<o:p></o:p>
<o:p> </o:p>
Set gcnn = New ADODB.Connection<o:p></o:p>
gcnn.ConnectionString = mstrOLEDBConnect<o:p></o:p>
gcnn.ConnectionTimeout = 0<o:p></o:p>
gcnn.CommandTimeout = 0<o:p></o:p>
gcnn.Open<o:p></o:p>
<o:p> </o:p>
myPriData = "SELECT PriJComCode, PriGroupCode, PriZd, PriItmWinsorK "<o:p></o:p>
myPriData = myPriData & "FROM ReportData "<o:p></o:p>
myPriData = myPriData & "WHERE PriScrID BETWEEN 721 AND 725 "<o:p></o:p>
myPriData = myPriData & "AND PriItmCode = 0 "<o:p></o:p>
myPriData = myPriData & "ORDER BY PriJComCode, PriGroupCode, PriZd DESC "<o:p></o:p>
<o:p> </o:p>
Set rst = gcnn.Execute(myPriData)<o:p></o:p>
<o:p> </o:p>
Worksheets("Sheet1").Range("a2").CopyFromRecordset rst<o:p></o:p>
<o:p> </o:p>
=============================================================<o:p></o:p>
<o:p> </o:p>
I am working (but its getting very difficult) on a VBA code that will change the SQL code through an input box i.e.<o:p></o:p>
<o:p> </o:p>
For example, I may want to change the SQL query from:<o:p></o:p>
<o:p> </o:p>
myPriData = myPriData & "WHERE PriScrID BETWEEN 721 AND 725 "<o:p></o:p>
<o:p> </o:p>
to:<o:p></o:p>
<o:p> </o:p>
myPriData = myPriData & "WHERE PriScrID BETWEEN 1000 AND 2000 "<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
by using an input box instead of going the the VBA code to change.<o:p></o:p>
<o:p> </o:p>
Please kindly assist.<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Kind Regards


Phresh10
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Perhaps something like:
Code:
Dim strFirst As String
Dim strLast As String
Dim Message As String
Dim Title As String
Dim Default As String
' replace the following line:
'myPriData = myPriData & "WHERE PriScrID BETWEEN 721 AND 725 "
' with the following lines:
Message = "Enter lowest number (between 500 and 1000)"
Default = "721"
Title = "Input number"
strFirst = InputBox(Message, Title, Default)
Message = "Enter highest number (between " & Str(CInt(strFirst) + 1) & " 1000)"
Default = Str(CStr(strFirst) + 1)
Title = "Input number"
strLast = InputBox(Message, Title, Default)
mypridata = mypridata & "WHERE PriScrID BETWEEN " & strFirst & " AND " & strLast & " "
With the wording changed as necessary.
You will also need to add some validation to make sure that the first number is in fact lower than the second number and that it is numbers that were entered etc.
 
Upvote 0

Forum statistics

Threads
1,225,155
Messages
6,183,211
Members
453,151
Latest member
Lizamaison

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