MS Access: Bound condition in endless Form via vba, - vba

I just try to create a calendar in MS-Access out of 42 unbound Forms, using them as endless Listes of entrys.
Now I like to fill them with entrys of different typ (group-appointments, entrys from Joe or from Marry etc.) with a recordset, createt for every of this 42 forms. And in the end, all this entry shall have a specific color, so Joe will get his in blue and Marry some in red.
So first I fill the recordset of the Form and than I run threw it like this:
Dim objFormatConditionGroup As FormatCondition
Dim objFormatConditionJoe As FormatCondition
Dim objFormatConditionMarry As FormatCondition
Dim ctl As Control
With Me
Do While Not .Recordset.EOF
Set ctl = .Topic
Set objFormatConditionGroup = ctl.FormatConditions.Add(acExpression, , "'" & .Recordset.typ & "' = '1'")
objFormatConditionGroup.BackColor = enmColor.Red
objFormatConditionGroup.ForeColor = enmColor.White
Set objFormatConditionJoe = ctl.FormatConditions.Add(acExpression, , "'" & .Recordset.typ & "' = '2'")
objFormatConditionJoe.BackColor = enmColor.Blue
objFormatConditionJoe.ForeColor = enmColor.White
Set objFormatConditionMarry = ctl.FormatConditions.Add(acExpression, , "'" & .Recordset.typ & "' = '3")
objFormatConditionMarry.BackColor = enmColor.White
objFormatConditionMarry.ForeColor = enmColor.Black
.Recordset.MoveNext
Loop
End with
Works somehow, but if I have multiple entry in one List, I always get the style of the last used block in for all entrys. Like: If there is a Groupappointment an something from Marry in one day (entrys are sortet by recordset.Typ), all entrys appear in Marry-Styl.
I gues, there is something wrong in the conditions-part of FormatConditions, but I can not figure it out. It shout be a String, "1=1" and "0=1" work like expectet. But how can I refere to the recordset of only one entry in the List?
Or how can I change the control .Topic of just one entry in the List and not all of them in the same step?

In FormatConditions, you can't refer to VBA variables or values. You need to refer to the actual column from the form recordsource.
E.g.
ctl.FormatConditions.Add(acExpression, , "[Typ] = '1'")
And don't do this in a recordset loop. Add each FormatCondition once. (As you have, but without the loop.)

Related

Run-Time error 3075 Access Syntax Error for combobox Search in Access

I am learning about how to create a searchbox with combo box. I was learning with a video in youtube :
Access: How to Create Search Form Using Combo box Part 1
However, when I do my code it doesn't work. :/ I get the Run-Time error 3075 Access Syntax Error.
Private Sub cboVendorSearch_AfterUpdate()
Dim MyVendor As String
MyVendor = "Select * from Vendors where ([vend_name] = " & Me.cboVendorSearch & ")"
Me.Invoices_subform.Form.RecordSource = MyVendor
Me.Invoices_subform.Form.Requery
End Sub
Assuming vend_name is a text field, need apostrophe delimiters.
MyVendor = "SELECT * FROM Vendors WHERE [vend_name] = '" & Me.cboVendorSearch & "';"
Instead of setting RecordSource, an alternative is to set Filter property.
Me.Invoices_subform.Form.Filter = "[vend_name] = '" & Me.cboVendorSearch & "'"
Me.Invoices_subform.Form.FilterOn = True
Would probably be better to use vendor ID for search criteria. Explore multi-column combobox where the ID field is a hidden column but combobox uses the hidden column for its Value yet displays vend_name to user.

VBA ListView control - retrieve columns from selected item

I have a ListView object within my Microsoft Access database that is set to Report View. When a user selects a row from this list, I want to be able to open a secondary form and populate based on the selected values. I am running into trouble with the proper syntax for capturing the column values I need. I have searched high and low looking for information on this, however I have not been able to find anything that fits my particular criteria.
Here is the code in question:
Private Sub ListView1_ItemClick(ByVal Item As Object)
Dim FormID, FilingID, RowIndex As Integer
Dim FilingName As String
RowIndex = Item.Index
FormID = Item.SubItems(0)
FilingID = Item.SubItems(1)
FilingName = Item.SubItems(2)
If MsgBox("Do you want to open up filing " & FilingName & "?", vbYesNo, "Confirmation") = vbYes Then
DoCmd.OpenForm "frmFiling", acNormal, , , , , "FormID=" & FormID & ";FilingID=" & FilingID
End If
End Sub
This code keeps throwing an exception on the line FormID = Item.SubItems(0) stating that it is an invalid property value.
Does anybody know what I am doing wrong here, or have a workaround that accomplishes what I am trying to do?
In your line FormID = Item.SubItems(0) where you declared FormID as a variant, you are assigning an object to it, so VBA expects SET FormID = Item.SubItems(0).
However what you want is probably FormID = CInt(Item.SubItems(0).Text)
I changed the event from ItemClick to Click, and used ListView1.SelectedItem.ListSubItems(1).Text to extract the column value.
Thanks everyone for your comments.

Multi criteria filter in VBA (Not equal to)

I am using the below code in Blue prism for filtering in excel for multi criteria.
But i am not able to filter multi criteria for Not equal to scenario.
Dim wb As Object
Dim excel as Object
Dim range as Object
Try
wb = GetWorkbook(Handle, Workbook)
excel = wb.Application
range = excel.Range(FRange)
Dim listOfValues as Array
listOfValues = Split(FCriteria,";")
wb.worksheets(Worksheet).select
range.select
range.Autofilter(FCol,listOfValues,7)
Success = True
Catch e As Exception
Success = False
Message = e.Message
Finally
wb = Nothing
End Try
Please help me tweaking the script
I'm almost sure that there is no filter option to set a "negative list". You can specify either a (positive) list of values (this is what your code does so far, for this you have to set the 7 as third parameter), or you can give a maximum of 2 individual criteria (in Excel, choose "Custom Filter" to set them.
You should play with the filter directly in Excel and try to set it like you want. Once you are satisfied with it, clear the filter, record a macro and repeat the filtering. Go to the VBA editor and see what's in there. It is straightforward to translate this into C# code.
But:
It's not possible to set any filtering by code (neither C# nor VBA) that you cannot set via the Excel GUI
I would question what you are trying to do. Since you are using Blue Prism, you should be trying to access the underlying data in a BP Collection(VB DataTable), rather than applying a filter, which is a visual tool for humans to further play with the interface. The robot will still have to do something with the filtered data, and it far easier to write code to proceed with data during the loop.
Otherwise use the Filter Collection Page of the 'Utilities - Collection Manipulation' VBO to get a filtered collection.
Also you are using VBA Split function, when you should use Split in VB as a method of the String.
Try this for a new page in the 'Utilities - Collection Manipulation' VBO(untested):
Dim NewRow As DataRow
Collection_Out = Collection_In.Clone
Dim Select_Concat As String
Select_Concat = "NOT(" & fieldName & " = '" & [String].Join("' OR " & fieldName & " = '", FCriteria.Split(";"c)) & "')"
For Each parentRow As DataRow In Collection_In.Select(Select_Concat)
NewRow = Collection_Out.NewRow
For Each c As DataColumn In NewRow.Table.Columns
NewRow(c.ColumnName) = parentRow(c.ColumnName)
Next c
Collection_Out.Rows.Add(NewRow)
Next parentRow
NewRow = Nothing
Collection_In = Nothing
Inputs: Collection_In(Collection), fieldName(Text), FCriteria(Text)
Outputs: Collection_Out(Collection)
You first need to get the entire range into an unfiltered Collection(which will be your Collection_In to this page, and then get the filtered Collection out....

Can't Go To Specific Record on Continuous Form in MS Access

Searched and searched and cannot find a solution to this. I have a form with many continuous form subforms. When I change a value in, lets say FIELD_A on one of the subforms, I run calculations on several other subforms, then the focus returns to FIELD_A. However, during the calculations an update to the PARENT form happens, and needs to happen. So, when I return focus to the original subform, the first record on my subform has the focus. I need to then go to the record I was working on.
I've tried several options, but nothing works. However, if I set a DEBUG breakpoint at the line in the code where it moves to the specified record, then physically RUN the code from that line, it works! I've tried setting a wait period in there to no avail.
Here's a snippet of the code:
Call common.CalculateAllLoadTotals _
(Me, Me.AffiliateID, Me.ClientID, Me.FacilityID, Me.ProposalRevision)
Me.Recordset.FindFirst "[AffiliateID] = '" & Me.AffiliateID & "'" & _
" AND [ClientID] = " & Me.ClientID & _
" AND [FacilityID] = " & Me.FacilityID & _
" AND [ProposalRevision] = " & Me.ProposalRevision & _
" AND [EquipmentID] = " & currItemID
I also tried this:
dim currRecord as Long
currRecord = Me.CurrentRecord
' >>> REST OF CODE HERE <<<
Call common.CalculateAllLoadTotals _
(Me, Me.AffiliateID, Me.ClientID, Me.FacilityID, Me.ProposalRevision)
Me.Form.Recordset.Move = currRecord
As I said, the code works (either one) IF I pause it with a debug then continue executing. Strange.
Sorry that's will not be a complete answer but it is quite lot for a comment.
Regarding your first solution - I'd advise you to try Me.Recordset.Requery
to refresh current record in the main form without moving position.
Regarding you 2nd option - I'd advise to have a look at bookmarks - before update remember bookmark, make some calculations and then move position to the saved bookmark. Something like this -
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[MyPK]=" & Me!cmbFindByPK
If Not rs.NoMatch Then
If Me.Dirty Then
Me.Dirty = False
End If
Me.Bookmark = rs.Bookmark
End If
Set rs = Nothing
taken from here Why clone an MS-Access recordset?
I suspect you are updating the recordset underlying the parent form. This causes the parent form to automatically requery, hence the subform position returning to the first record.
The solution is to update the controls on the parent form instead:
Me.Parent!Controlname = XXXX

Open new instance of a form based on data in field of first form

I have a data entry form where the data clerk enters client ID among other things. Client ID's are unique to each client. I am currently trapping for duplicate ID's and allowing the clerk to go to the search form and seach for the duplicate ID to verify that it is indeed the same person and not an error inputting data. I would rather open a new instance of the data entry form based on the client ID inputted into the data entry form. I can open a new instance but am not sure how to make it display the client data based on the client ID.
There is no good way to do this except to just turn off the screen painting. Here's some code from one of my apps:
Dim frm As Form_frmInventory
Dim strRecordsource As String
Dim intType As Integer
DoCmd.Hourglass True
Application.Echo False
Set frm = New Form_frmInventory
frm!boxHeader.BackColor = 3276900 ' 5483007
frm!boxFooter.BackColor = 3276900 ' 5483007
strRecordsource = "SELECT qryInventoryForm.*, varZLStoNull(IIf([tblInventory].[InventoryClass] In ('BKS','FAC','MTH','MUS','REF','SSC'),[Creator] & [Dates] & OtherAuthors([OtherAuthors]))) AS BibCreator, CreatorDates([Birth],[Death],[OtherAuthors]) AS Dates, varZLStoNull(Trim(nz(UCase([tblBib_Authors].[LastName]) & IIf(Not IsNull([tblBib_Authors].[FirstName]),', ') & [tblBib_Authors].[FirstName],'Anon.'))) AS Creator, tblBib_Authors.CreatorCategories, Nz([CreatorSort],[LastName] & [FirstName]) AS NameSort FROM qryInventoryForm LEFT JOIN tblBib_Authors ON qryInventoryForm.CreatorID = tblBib_Authors.CreatorID WHERE ([quantity]>0 Like getSold()) AND (qryInventoryForm.InventoryID=" & lngInventoryID & ") ORDER BY Nz([CreatorSort],[LastName] & [FirstName]), InventoryClass, ShortTitle;"
frm.RecordSource = strRecordsource
' need to change the caption and disable certain things
frm.Caption = frm.Caption & " -- " & frm!InventoryClass & "-" & Nz(frm!InventoryNo, Format(frm!InventoryID, "00000"))
frm!fldShortTitle.SetFocus
frm!cmbClassFind.Enabled = False
frm!cmbCreatorFind.Enabled = False
frm!cmbInventoryNumber.Enabled = False
[etc.]
frm.Visible = True
GoTo exitRoutine
CloseForm:
Call CloseForm(Me, True)
exitRoutine:
Application.Echo True
DoCmd.Hourglass False
Exit Sub
The CloseForm() sub is pretty important, as you have to keep track of multiple instances of the form and close the right one. I got the code from the ADH97 and adapted it from there (just the basics).
It would appear from that code (I've forgotten the details) that a form instantiated with Set frm = New Form_frmInventory is not visible until you explicitly reveal it. That's a plus, as it should mean that you don't have to turn off the screen (i.e., Application.Echo False), but I'm pretty sure that I recall needing it anyway to make things appear smoothly. My memory is that the form would appear with its normal colors and then the background colors would change visibly as the code ran. This means it was visible, so I'm not sure why it's necessary to then explicitly set the form visible.
Anyway, that should get you started, I think!