loop through listfield in access - ms-access

I need some help to create a loop through a listfield.
listfield:
lstPlanung
function:
Dim i As Integer
For i = 0 To Me!lstPlanung.ListCount - 1
Me!lstPlanung.Selected(i) = True
Call sendemailKunde
Next i
Is it possible to modify the loop that it selects every entry of my listfield?

It's unclear.
Is it this you are trying to do ?
Dim i As Integer
dim strFoo as string
For i = 0 To Me!lstPlanung.ListCount - 1
' Get the current listbox item in the loop
strFoo = Me.lstPlanung.ItemData(i)
Call sendemailKunde
Next i
With this, strFoo will contain the element of your listbox at each iteration in your loop. But you have to do something with it...

Related

How to populate a dynamic array in MS Access?

I have a dynamic array that I want to append values to. The number of values to be appended is not fixed
I was trying to do something like this:
Dim array() As Integer
ReDim Preserve array(UBound(array)+1)
bulkJob(UBound(array) + 1) = Me.ID
I get subscript out of range error at ReDim Preserve array(UBound(array)+1). Is there a way to do this?
Not quite clear what you are trying to do, but this could get you some ideas:
Public Function BuildJobs(Id As Integer)
Static bulkJob() As Integer
Dim Upper As Integer
On Error Resume Next
Upper = UBound(bulkJob) + 1
On Error GoTo 0
ReDim Preserve bulkJob(Upper)
' Fill in value.
bulkJob(Upper) = Id
' Do something.
Debug.Print UBound(bulkJob), bulkJob(Upper)
End Function
"Restart" the array like this:
ReDim bulkJob(0)
bulkJob(0) = 0

Moving to position of multi select listbox

I have a multi select listbox, which I would like to make searchable. If the searched for value is found in the listbox, I'd like to scroll to that position, but not select it. Is this possible? The code I have so far for searching is :-
With lstComm
For i = 0 To .ListCount - 1
If .Column(6, i) = txtSearch.Value Then
End If
Next i
End With
...but I'm not sure how to complete the scroll.
This should work fine:
Dim index As Long
With lstComm
Dim match As Boolean
For index = 0 To .ListCount - 1
If .Column(1, index) = txtSearch.Value Then
match = True
Exit For
End If
Next
If Not match Then Exit Sub
Dim isSelected As Boolean
isSelected = .Selected(index)
.Selected(index) = True
.Selected(index) = isSelected
End With
It retrieves the searched item of the listbox.
If no item has been found it exits.
Otherwise it stores the current selection state of that item, selects it to position the listbox, and restores the stored state of the item.

Access For Loop Hide Objects

I'm trying to hide/unhide around 30 objects on my form when the user selects certain values from a dropdown menu. I tried the loop below, however I receive the following error: 'Object doesn't support this property or method.' I have this code running on the 'AfterUpdate' of the dropdown menu object.
Dim VisibleVisitFields() As String
Dim VisibleVisitFieldlist As String
Dim varVisibleVisit As Variant
VisibleVisitFieldlist = "VisitDate_Event,VisitTime_Event,VisitSite_Event,VisitStaff_Event,VisitMeet_Event"
VisibleVisitFields = Split(VisibleVisitFieldlist, ",")
If (EventType = 3) Then
For Each varVisibleVisit In VisibleVisitFields
[Forms]![subFRM_TBL_Event-All in One].Controls(varVisibleVisit).visible = True
Exit For
Next
Else
If (EventType <> 3) Then
For Each varVisibleVisit In VisibleVisitFields
[Forms]![subFRM_TBL_Event-All in One].Controls(varVisibleVisit).visible = False
Exit For
Next
End If
End If
Which line triggers the error? Suspect it is reference to the subform that is flawed. Never seen code like that to loop through an array. Suggest naming subform container different from the object it holds, such as ctrEvent. What is EventType - a textbox/field on the form? Consider code:
Dim aryFields As Variant
Dim x As Integer
aryFields = Split("VisitDate_Event,VisitTime_Event,VisitSite_Event,VisitStaff_Event,VisitMeet_Event", ",")
For x = 0 To UBound(aryFields)
Me.ctrEvent.Form.Controls(aryFields(x)).Visible = Me.EventType = 3
Next
Alternative methods not using array:
Set control Tag property then code loops through all controls on form and sets visibility for those that have particular value in Tag.
Dim ctrl As Control
For Each ctrl in Me.ctrEvent.Form.Controls
If ctrl.Tag = "something" Then ctrl.Visibility = Me.EventType = 3
Next
Another is to give controls similar names, like: Visit1, Visit2, etc. Then code:
Dim x As Integer
For x = 1 to 30
Me.ctrEvent.Form.Controls("Visit" & x).Visible = Me.EventType = 3
Next
Advise no spaces or punctuation/special characters (underscore only exception) in naming convention.
You are trying to iterate over an array of strings.
Dim VisibleVisitFields() As String
You need to declare the array to contain variants (which can still contain strings)
Dim VisibleVisitFields() As Variant

MS Access - How to Add Items to List Box and Select only Items that are found in Recordset

I have a list box that is populated by a recordset. I am trying to then select the items in that list box based on the values in another recordset. I am able to populate the list box, but when I try to select the values based on another recordset the list box Me.ToolUsed1 is Null. I call another function for selecting the values because I plan on using the same procedure for other list boxes. I really appreciate any help that you can provide.
'Populate the tool list box
While Not rsToolList.EOF
Me.ToolUsed1.AddItem Item:=rsToolList.Fields(0)
rsToolList.MoveNext
Wend
matchKey = "MatchKey = """ & rsActivities.Fields(0) & """"
If rsTools.RecordCount > 0 Then
rsTools.MoveFirst
rsTools.FindFirst (matchKey)
toolIndex = rsTools.Fields(2)
While Not rsTools.EOF
If (rsTools.Fields(2) = toolIndex) Then
SelectListValues Me.ToolUsed1, rsTools.Fields(1)
End If
rsTools.MoveNext
Wend
End If
Private Sub SelectListValues(tempListBox As Object, selectString As String)
Dim i As Integer
Dim found As Boolean
i = 0
found = False
'select the value in the listbox
While i < tempListBox.ListCount And Not found
If tempListBox.Value(i) = selectString Then
tempListBox.Selected(i) = True
found = True
End If
i = i + 1
Wend
'if the string wasn't found, add it
If Not found Then
tempListBox.AddItem (selectString)
End If
End Sub
Consider using a query recordsource for your listbox instead of value items to add. Listboxes like comboxes maintain the RowSource property, allowing for Table/Query sources which you can set to the first recordset, rsToolList. Then, just open one recordset, rsTools, and loop through it to decide selected items. Do note, with table/query sources the bound column is the value of the listbox, not any of the other columns.
' POPULATE TOOL LIST BOX TO QUERY
Me.tempListBox.RowSource = "ToolList" ' OR USE SELECT SQL STATEMENT HERE
Me.tempListBox.RowSourceType = "Table/Query"
Me.tempListBox.Requery
' LOOP THROUGH LISTBOX AND RECORDSET FOR SELECTED ITEMS
Dim rsTools As Recordset, i As Integer
Set rsTools = CurrentDb.OpenRecordset("Tools", dbOpenDynaset)
rsTools.MoveLast
rsTools.MoveFirst
If rsTools.RecordCount > 0 Then
While Not rsTools.EOF
i = 1
While i < Me.tempListBox.ListCount
' CHANGE C FUNCTION HERE TO NEEDED TYPE: CLng, CInt, CStr, CDate, ...
If CLng(Me.tempListBox.ItemData(i)) = rsTools.Fields(1) Then
Me.tempListBox.Selected(i) = True
End If
i = i + 1
Wend
rsTools.MoveNext
Wend
End If
rsTools.Close
Set rsTools = Nothing

Unable to edit msgraph seriescollection

I am pulling out my hair trying to parse data or edit into a msgraph series collection.
I get error 438 - object does not support this property or method.
I can manipulate other properties that the object has such as ChartTitle.Font.Size but not the seriescollection.
Intellisencing is not working wth this object which leads me to susspect that I have not set a particular reference.
Sections of the code is below.
The main routine gets the object:
strReportName = "Security Selection"
strChartName = "MACD_Chart"
DoCmd.OpenReport strReportName, acViewDesign
Set rptMACD = Reports(strReportName)
Set chartMACD = rptMACD(strChartName)
A data recordset is built then all of it is passed into the subroutine:
Call UpdateChart(chartMACD, rstMACD)
Public Sub UpdateChart(chartPlot As Object, rstChart As ADODB.Recordset)
'FUNCTION:
' a chart object is passed into the routine,
' source data is update to the recordset being passed in.
Dim lngType As Long
Dim i, j, iFieldCount As Integer
Dim rst As Recordset
Dim arXValues() As Date
Dim arValues() As Double
Dim strChartName, strYAxis, strXAxis As String
Dim ChrtCollection As ChartObjects
Dim colmCount As Integer
chartPlot.RowSourceType = "Table/Query"
'get number of columns in chart table/Query
iFieldCount = rstChart.Fields.Count
With chartPlot
'change chart data to arrays of data from recordset
.Activate
j = 0
rstChart.MoveFirst
Do While Not rstChart.EOF
j = j + 1
ReDim Preserve arXValues(1 To j)
arXValues(j) = rstChart.Fields("Date").Value
rstChart.MoveNext
Loop
For i = 1 To iFieldCount - 1 'Date is first field
j = 0
rstChart.MoveFirst
Do While Not rstChart.EOF 'get next array of data
j = j + 1
ReDim Preserve arValues(1 To j)
arValues(j) = rstChart.Fields(i + 1).Value
rstChart.MoveNext
Loop
.SeriesCollection(i).Name = rstChart.Fields(i + 1).Name
.SeriesCollection(1).XValues = arXValues
.SeriesCollection(i).Values = arValues
Next i
end sub
I've tried many things and now I'm totally confused. I've also been trying to parse in recordsets (which is my preference) but i'll take anything at the moment.
Before continuing: I recommend setting the Chart's Rowsource property to a query that returns the data you want and then Requerying the Chart. This is WAY easier than the following.
You are getting the Error 438 because Name, XValues, Values are not properties of the Series Object. MSDN Info
That being said, here is a go at your method and some recommendations for doing it that way. The SeriesCollection doesn't contain the values associated with MSGraph points like it does in Excel. You need to edit the data in the DataSheet, which is VERY finicky. A reference to the Microsoft Graph Library must be included. This was tested to work with my database. Microsoft Graph MSDN info
DAO
Public Sub testing()
Dim rstChart As Recordset
Dim seri As Object, fld As Field
Dim app As Graph.Chart
chartPlot.SetFocus
Set app = chartPlot.Object
Set rstChart = CurrentDb.OpenRecordset("SELECT DateTime, ASIMeasured FROM Surv_ASI WHERE CycleID = 2 ORDER BY DateTime")
app.Application.DataSheet.Range("00:AA1000").Clear
With rstChart
For Each fld In .Fields
app.Application.DataSheet.Range("a1:AA1").Cells(0, fld.OrdinalPosition) = fld.Name
Next
Do While Not .EOF
For Each fld In .Fields
app.Application.DataSheet.Range("a2:AA1000").Cells(.AbsolutePosition, fld.OrdinalPosition).Value = fld
Next
.MoveNext
Loop
End With
app.Refresh
End Sub
ADO (Assuming rstChart is already a valid ADODB.Recordset)
Public Sub testing()
Dim app As Graph.Chart, i As Integer
chartPlot.SetFocus
Set app = chartPlot.Object
app.Application.DataSheet.Range("00:AA1000").Clear
With rstChart
.MoveFirst 'Since I don't know where it was left off before this procedure.
For i = 0 To .Fields.Count - 1
app.Application.DataSheet.Range("a1:AA1").Cells(0, i) = .Fields(i).Name
Next
Do While Not .EOF
For i = 0 To .Fields.Count - 1
app.Application.DataSheet.Range("a2:AA1000").Cells(.AbsolutePosition, i).Value = .Fields(i)
Next
.MoveNext
Loop
End With
app.Refresh
End Sub
Some notes about my changes:
1. I prefer having my With point to the Recordset being cycled, instead of the Object being operated on, especially since more calls are made to the Recordset's properties in your procedure.
2. You don't need to specify the variable to which a Next applies (Next i). Just put Next.
3. Please pick my answer if it helped :)