VB6 populate a list box using adodb recordset - listbox

I had a form which originally used Listview control but I need to change it into Listbox control. Now here's the original code which use for listview. I'm trying to change it for populate listbox using adodb recordset but keep getting errors. lv1 is the listbox1; lv2 is the listbox2. Listbox doesn't have listitem.
Private Sub PopulateListView()
Dim cnPop As ADODB.Connection
Dim rsPop As ADODB.Recordset
Dim lst As ListItem
Dim lngRecs As Long
On Error GoTo ErrPopulate
Set cnPop = New ADODB.Connection
cnPop.CursorLocation = adUseClient
cnPop.Open gcnORA
Set rsPop = New ADODB.Recordset
rsPop.Open sSQL, cnPop, adOpenDynamic, adLockReadOnly
lv1.ListItems.Clear
Do While rsPop.EOF = False
Set lst = lv1.ListItems.Add(, , rsPop!Customer_Number)
lst.SubItems(1) = rsPop!Customer_Name
rsPop.MoveNext
Loop
If rsPop.RecordCount > 0 Then
SelectButtons True
End If
With lblCount
.Caption = Format((rsPop.RecordCount), "#,##0") & IIf((rsPop.RecordCount) <= 1, " Customer", " Customers") & " found"
.Refresh
End With
rsPop.Close
cnPop.Close
Set rsPop = Nothing
Set cnPop = Nothing
Exit Sub
ErrPopulate:
MsgBox Err.Description, vbCritical, "Populate Error"
End Sub

A listview can have multiple columns, you apparently had at least two columns in your previous listview: Customer Number and Customer Name.
A listbox only has one column. You'll need to decide how to display/add your two columns of data to the listbox. Don't be confused by the columns property of the listbox, when set to multiple columns, the control still only has a single column of data - it simply snakes and scrolls horizontally instead of scrolling vertically.
Adding an item to the listbox:
lv1.Additem rsPop!Customer_Name
Listbox documentation: VB6 Listbox

Related

Call the name of a Texbox with number changing in Visual Basic 6

I'm use Visual Basic 6 to create a table with many Textbox which named txtNo1, txtNo2, txtNo3,...
I want to use the "For...Next..." loop to assign a content to these Textbox.
How can I call all these Textbox in the simplest way?
For i = 1 to 100
txtNo (......) .txt = "ABC"
Next i
Instead of using unique textboxes, each with a unique name, you should use a (textbox) control array:
Place the 1st textbox on the form, name it 'txtNo'
Copy it and paste it onto the form
VB will ask you "There's already a control named 'txtNo'. Would you like to create a control array?". Answer "Yes"
Paste as the textbox as many times as you need it
Then your code looks like
' Control arrays typically start at index 0
For i = 0 to 100
txtNo(i) .txt = "ABC"
Next i
Jim Mack's solution works as well, code for it:
' Assuming your form is named 'Form1'
For each ctrl in Form1.Controls
If TypeOf ctrl Is Textbox
For i = 1 To 100
If ctrl.Name = "txtNo" & CStr(i) Then
ctrl.Text = "ABC"
End If
End If
End If
It's a bit more complex, but therefore more flexible as works with multiple control types (in one loop).
If you need an easiest way to create your textboxes as a table, you can Load the controls at runtime. You have to add only the first TextBox control to your form, set the name to "txtNo", and Index to 0 in the Properties window.
In your code, call Load() to create additional controls, and you can set the Top/Left and other properties
For i = 1 To 100
Load txtNo(i)
txtNo(i).Top = txtNo(i - 1).Top + txtNo(i - 1).Height + 150
txtNo(i).Left = txtNo(i - 1).Left
txtNo(i).Text = "Textbox " & i
txtNo(i).Visible = True
Next i
If you need again to change any control property, from your list of controls, you can iterate only over your control list, instead of all controls of your Form
For i = txtNo.LBound() To txtNo.UBound()
Form1.Controls("txtNo")(i).Text = "New text " & i
Next i

Breaking Links to Excel for PowerPoint Charts

I wrote a code to break links to the source excel file for my powerpoint deck, the macro works well except for 2 charts. Both the charts are on the same slide (which is not uncommon) and are line charts. The charts need to be manually updated but the data resides in a excel file only. Not sure what am I missing. This is the code that I wrote
Sub SavePPT()
Dim objPP As Object
Dim objPPFile As Object
Dim sld As Object
Dim shp As Object
Dim shp1 As Chart
Dim newshp As Shape
Dim pptChart As Object
Set objPP = CreateObject("PowerPoint.Application")
objPP.Visible = True
Set objPPFile = objPP.ActivePresentation
objPPFile.Save
Application.EnableEvents = False
For Each sld In objPPFile.Slides
For Each shp In sld.Shapes
If shp.HasChart Then
shp.LinkFormat.BreakLink
On Error GoTo 0
End If
Next
Next
Application.EnableEvents = False
For Each sld In objPPFile.Slides
For Each shp In sld.Shapes
If shp.Type = msoLinkedOLEObject Or shp.Type = msoEmbeddedOLEObject Then
shp.LinkFormat.BreakLink
On Error GoTo 0
End If
Next
Next
objPPFile.SaveAs ("Location" _ & Format(Now(), "MM-DD-YYYY") & ".pptx")
objPPFile.Close
objPP.Quit
Set pptChart = Nothing
Set objPPFile = Nothing
Set objPP = Nothing
End Sub
The chart might be in a placeholder, in which case none of your code will act upon it. If the shape's .Type = msoPlaceholder (ie, 14), check the shape's .PlaceholderFormat.ContainedType property to see if it's a linkedOleobject or .HasChart etc.
BTW, an msoEmbeddedOLEObject won't have a .LinkFormat object to call on; I suspect you have some error handling code that's obscuring an error there. In any case, I'd limit that check to just msoLinkedOLEObject.
Thanks for you help. I realized it later that those 2 slides have a lot of text boxes on them and when I consolidate some of those textboxes the issue was resolved.

Radstudio ListView and FireDac Binding Synch Issue

I have a table with a primary key (ID) and a unique field (REF) that I want to bind to a ListView.
With LiveBindings the links are created with :
1) (Table) * <-> Synch (ListView)
2) (Table) ref -> Item.Text (ListView)
Additionaly, a link are created with other component:
3) (Table) ref -> Text (ListBoxItem1)
ALL WORKS FINE if IndexFieldNames of Table are ID.
But I want to sort records by REF and when I set IndexFieldNames = ref the selected table record is always the PREVIOUS selected item on ListView, this means:
At same time that the selected ListView.Item.Text = "Ref of Record no. 2" the ListBoxItem1.Text = "Ref of Record no. 1".
I'm new with live bindings, did I make some mistake?
Before set IndexFieldNames, You need call the procedure ResetNeeded of TBindSourceDB component
I only get a workaround:
1) Do NOT connect synch of listview;
2) IndexFieldNames = ref
3) Inside event OnClick (ListView) use a Table.Locate to set the correct clicked record.

Transfer a data set from openoffice base to calc

After I did a query in openoffice-base over a customized form I want to transfer a selected set of data into a template openoffice-calc table. I know I can access the data set in openoffice-calc via pressing the Data Source (F4) button but then I only get access over the query. The best solution would be after the database query over a form a button event is required to open a openoffice-calc table from the template and insert the data from the data set.
First go to Tools -> Macros -> Organize Macros -> LibreOffice Basic and add this code. Change the path of the template file.
Sub Copy_Record_To_Calc(oEvent)
Dim oForm
Dim templatePath As String
Dim oServiceManager As Object, oDesktop As Object
Dim oFileProperties As Object
Dim oDoc As Object, oSheet As Object, oCell As Object
Dim column As Integer
oForm = oEvent.Source.getModel().getParent()
If oForm.isAfterLast() Then
Print "Hey, you are after the last element."
Exit Sub
ElseIf oForm.isBeforeFirst() Then
Print "Hey, you are before the first element."
Exit Sub
End If
templatePath = "file:///C:/Users/JimStandard/Desktop/Untitled 2.ots"
Set oServiceManager = CreateObject("com.sun.star.ServiceManager")
Set oDesktop = oServiceManager.createInstance("com.sun.star.frame.Desktop")
Set oFileProperties(0) = new com.sun.star.beans.PropertyValue
oFileProperties(0).Name = "AsTemplate"
oFileProperties(0).Value = True
Set oDoc = oDesktop.loadComponentFromURL( _
templatePath, "_blank", 0, Array(oFileProperties))
oSheet = oDoc.Sheets(0)
For column = 1 to 2
oCell = oSheet.getCellByPosition(column - 1, 0)
oCell.String = oForm.getString(column)
Next column
End Sub
Then in form design mode, right-click on the button and choose Control. In the Events tab, click the three dots next to Execute action. Click Macro... and find the Copy_Record_To_Calc macro that you added.
Now turn design mode off. Go to a record and click the button. It will open the Calc template and copy the first two columns of the current record into column A and B of the spreadsheet.
See also:
Section 4.2.1 of Andrew Pitonyak's Base Macros (PDF)
ResultSet documentation
This thread gives an example of using a Calc template.

VB- For Each ctl , Textboxes not being recognized

I cannot figure out why the code below will not work. The first list box displays all of the controls except the textboxes. Of course, the MsgBox is doing nothing - since it is only looking for textboxes. Why is it not recognizing the textboxes? By the way, I have changed the names on some of my textboxes because I have so many. Can it not recognize what type of control it is if I have changed the name?
Dim ctl As Control
For Each ctl In Me.Controls
ListBox1.Items.Add(ctl.Name)
If TypeOf ctl Is TextBox Then
MsgBox(ctl.Name)
'validate that it is numeric
If ctl.Text = "" Then
'if not show error and exit sub
MessageBox.Show("Please fill all blanks.")
Exit Sub
End If
End If
Next
I appreciate the help.

Resources