User Form Help Please

mercmannick

Well-known Member
Joined
Jan 21, 2005
Messages
730
Hi

i would like to create a userform with a textbox to enter a product code , a command button to run VBA and a big text box to show output of VBA from Textbox 1 which i will connect to DB and pull back information relevant to that product code , is this possible i can create the userform no problem and already have code to pull back from DB into Excel , can anyone give me a idea how to do this if its atall possible

Thanks

Nick
 
Hi Code works fantastic to put active cell text in textbox1 , how do i call my code when command button is clicked or even auto when userform is shown , to show results in listbox1

Anyone give me any pointers please

Thanks

Nick
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Code:
UserForm1.ListBox1.List = ssSQL

am i heading in right direction adding this line to my routine to add results to listbox1

Thanks

Nick
 
Upvote 0
Nick

Short answer - no.

ssSQL (or whatever the variable is called) is just a string containing the SQL code for your query.
 
Upvote 0
Nick

Short answer - no.

ssSQL (or whatever the variable is called) is just a string containing the SQL code for your query.

Hiya Norie

well bang goes my idea then hehe , how would be best way of doing this

Regards

Nick
 
Upvote 0
Nick

Well if we knew what your idea was we might be able to help.:)

Do you know how to populate a multicolumn listbox from a range on a worksheet?

Try this.

1 Create a userform.

2 Add a listbox to it.

3 Add 2 command buttons to it.

4 Double click one of the command button and add this code, replacing any already there.

5 Run the form and see what happens.

Code:
Option Explicit
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim rng As Range
Dim NoCols As Long
Dim NoRows As Long
    Set ws = Worksheets.Add
    
    Set rng = ws.Range("A1")
        
    ' let's create some random data to work with
    ' you probably want to replace this with real data
    
    NoCols = Int(Rnd * 5) + 1
    
    NoRows = Int(Rnd * 20) + 1
        
    With rng.Resize(NoRows, NoCols)
        .Formula = "=CHAR(CODE(""A"")+column()-1) & INT(RAND()*100)+1"
        .Value = .Value
    End With
    
    With Me.ListBox1
        .ColumnCount = NoCols
        .ColumnWidths = Join(Split(String(NoCols, ";"), ";"), "30;")
        .List = rng.CurrentRegion.Value
        .MultiSelect = fmMultiSelectMulti
    End With
        
End Sub
 
Private Sub CommandButton2_Click()
    Unload Me
End Sub
 
Upvote 0
Nick

You don't - you amend your code to put the results onto the worksheet instead of random data as I did.

And you've already got that code - CopyFromRecordSet.
 
Upvote 0
Code:
Public Sub Routeuserform()
    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim fld As ADODB.Field
    Dim MyConn
    Dim i As Long, intLastRow As Long
    Dim ShDest As Worksheet
    Dim ssSQL As String
    Dim rng As Range
    Dim Cell As Variant
    Dim whereString
    Dim x As Integer
    Dim varrecords As Variant

    Set ShDest = Sheets("TEST")
            
    ssSQL = " SELECT [Sql Man Plan].[CW Drg] AS [Cw No], [FN Routes].description AS Details, [FN Routes].hours AS [EST Hours]" _
         & " FROM [FN Routes] INNER JOIN [Sql Man Plan] ON [FN Routes].[file no] = [Sql Man Plan].[File No]" _
         & " WHERE ((([Sql Man Plan].[CW Drg])='" & UserForm1.TextBox1.Value & "'))"
         

    Set cnn = New ADODB.Connection
    MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
    
    With cnn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .Open MyConn
    End With

    Set rst = New ADODB.Recordset
    rst.CursorLocation = adUseServer
    rst.Open Source:=ssSQL, ActiveConnection:=cnn, _
             CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic, _
             Options:=adCmdText
    'i want to populate userform listbox now
    rst.MoveLast

   'Count the number of records that are being returned and assign that
   'number to a variable.
      x = rst.RecordCount

      'Move the record pointer to the first record. This is required in
      'order to return the number of records specified by the RecordCount
      'Property. If this is not done, the record pointer will remain on
      'the last record and only the last record will be returned.
      rst.MoveFirst

      'Return the records to an array variable.
      varrecords = rst.GetRows(x)

      'Fill the list box.
      UserForm1.ListBox1.List = Array(varrecords)
        
      
    ' Close the connection
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing

End Sub

Norie , i was trying this to get records into list box , runs through all the code and still my listbox1 is empty

Nick
 
Upvote 0
Nick

That's the first time you've posted any code using GetRows.

That's a different thing than using CopyFromRecordSet.

I think you need to re-read the comments, particularly the bit about what GetRows does.

And then you might want to think about removing Array(...), you've already got one.:)
 
Upvote 0
Nick

That's the first time you've posted any code using GetRows.

That's a different thing than using CopyFromRecordSet.

I think you need to re-read the comments, particularly the bit about what GetRows does.

And then you might want to think about removing Array(...), you've already got one.:)

Code:
'Fill the list box.
      UserForm1.ListBox1.List = varrecords

**** so close, it is giving me the first two records but across the top so two rows but in 4 columns first record first twice then same on second record , like below


Machine Schedules Rev 3.xls
GHIJ
36721672167226723
4Cast-ArgonStirCast-ArgonStir1stHeatTreatment1stHeatTreatment
50000
TEST


i need it to come out like this

Machine Schedules Rev 3.xls
ABCD
11CwNoDetailsESTHours
126721Cast-ArgonStir0
1367211stHeatTreatment0
146721DryGrindbands,removegitandcutoffhead3
156721Driller-Machine75mmx90degtemporarycentrei1
166721RoughTurntoDHdrawingforSelas90
1767212ndHeatTreatment0
186721Preparebrlhardnessspots-5posns,8xround3
1967211stCtr-M/c75mmx90tempctr.Ifhead<350mm1
206721Ifbrlhardnessok,skimbrlfacesforhardness1.5
216721Ifbrlhardnessesok,thenskimbrldiato+3mm10
226721FinTurn.Viewtapers/forms.DyePenBrl.R/hform53.5
2367212ndCtr-faceendlengths&machinecentres14
246721DrillHolesforDrives.Ensurewillnotbreakthro2
256721Grindtodrg,using+0.03mmallowance18
266721Millcomplete20
276721Inspection&Fitting-CONFIRMallchecksdone4
286721Paint,ProtectandWraptospecification0
TEST


thanks

Nick
 
Upvote 0

Forum statistics

Threads
1,215,346
Messages
6,124,417
Members
449,157
Latest member
mytux

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