Create an immediate window style log on a form - ms-access

Is it possible to create something like the immediate window on a form?
I've tried sending various updates to a text box, but the update rate on text box seems to be much slower than what you get with Debug.Print to the immediate window.
Would be good to tell the user what operations are currently being carried out whilst they wait for something to be processed (and without skipping some operations because they are processed quicker than the update rate would seemingly allow).

You can set the textbox (or label) text and then call the DoEvents method.
This last call is important because is supossed you are executing a long process and screen will redraw only on idle time.
Alternatively, you can use the status bar to notify your progress:
SysCmd acSysCmdSetStatus, "Doing some long task, please wait..."
'Do your work
SysCmd acSysCmdSetStatus, ""
If you can measure your progress, it's possible to include a progress bar. I.e. processing a recordset:
bk = rs.Bookmark
rs.MoveLast
r = SysCmd(acSysCmdInitMeter, caption, rs.RecordCount)
rs.Bookmark = bk
Do Until rs.EOF
'Do something
i = i + 1
If i Mod 10 = 0 Then 'Do not update the meter for every processed record
SysCmd acSysCmdUpdateMeter, i
End If
rs.MoveNext
Next
SysCmd acSysCmdRemoveMeter

In the past I had tried using a single multi-line Text Box as a scrolling status window but it seemed to be inordinately fussy when it came to the text selection status (.SelText, .SelLength, .SelLength). In the end I just went with five (5) single-line Text Box controls and "scrolled" them myself.
In the following screenshot the Text Box controls are named txtStatus4, txtStatus3, ... txtStatus0 (from top to bottom)
and the code to update that status "window" (actually a Frame control) is
Private Sub UpdateStatus(StatusText As String)
Dim i As Long
For i = 4 To 1 Step -1
Me.Controls("txtStatus" & i).Value = Me.Controls("txtStatus" & i - 1).Value
Next
Me.txtStatus0.Value = StatusText
Me.Repaint
End Sub

Just in case anyone ever looks for this again, there IS a way to do what the OP asked, without multiple textboxes.
Screenshot of solution
On your form, add a textbox. A regular textbox will do. Give it a name, like txtLog. Size it to be big enough to display a bunch of text.
Here's a sub to handle the updating:
Private Sub WriteToLogText(strText As String, Optional blnClear As Boolean = False)
txtLog.SetFocus
If blnClear = True Then
txtLog = strText
Else
txtLog = txtLog.Text & Chr(13) & Chr(10) & strText
End If
End Sub
Then call the sub to write your text:
strMsg = "Warning: Commodity '" & Trim(drs("RawCommodity")) & "' is not mapped."
WriteToLogText strMsg

Related

Is it an access Bug? my problem with form filtering by txtBox entered value in a split DB

I have an Access 2016 DB 64bits contain related tables with a search split form that have an txtBox that filtered records accoring to its entered value.
Every thing is just fine with that for months and with thousands of records.
When I want to make it more proffesional to deploy it to other users by re-writing another similar DB but with 32bts Access 2016 and SPLIT it from the start into Tables Back-end and a Front end, the search form didn't show any record when I fire a searching parameter in the after update event!
the search form is linked to a query, I change it to a new one, also try to link the form directly to only one table for testing also not goes right, shows zero results.
when remove the filter, all records showing up again.
when I change the filter directly from the form properties it also fail for string search but when search for a primary key in a form control holding it its working just fine. ( search for Strings show no results, search for Numbers ( ex ID value, show correct results).
And Very strange, when I build another new DB (Same access application) just for testing for what is going on, with a form and joined to split back end tables and a textbox in the form to enter keywords to show the all related records in the that split form, it works fine, both for strings and for numbers?
Private Sub txtSearch_AfterUpdate()
Dim strFilter As String
On Error Resume Next
If Me.txtSearch.Text <> "" Then
strFilter = "[Complaints] Like '*" & Me.txtSearch.Text & "*'"
Me.Filter = strFilter
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
With Me.txtSearch
.SetFocus
.SelStart = Len(Me.txtSearch.Text)
End With
End Sub
when put a string into the txtsearch box , the code must search for any string with in [Complaints] similar to it and shows the records holding it.
with me it works in the old DB 64bits Not splitted, but its not working for the new DB 32bits split to back and front ends.
Be careful using .Text as it requires the control to have focus. So try:
Private Sub txtSearch_AfterUpdate()
Dim strFilter As String
' On Error Resume Next ' Don't use while debugging.
If Nz(Me!txtSearch.Value) <> "" Then
strFilter = "[Complaints] Like '*" & Me!txtSearch.Value & "*'"
Me.Filter = strFilter
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
With Me!txtSearch
.SetFocus
.SelStart = Len(.Value)
End With
End Sub

Hyperlink text disappears based on combo box value on continuous form

I'm having a bit of an unusual problem (or at least I think it's unusual). I have a continuous form that shows certain records based on a query that updates after I change the value in a combo box. I have a "View All" hyperlinked text box so that I can switch to seeing all of the records when I want to, but have discovered it only appears when I have selected a combo box value that has records. For example, if I want to find records assigned to Joe Shmo and there is one record assigned to him, the hyperlink appears. If I want to find records assigned to Susie Seashell and she has none assigned to her, it disappears. The catch is that if I click on the area of the screen where the hyperlink should appear, it still works as expected-- there is just nothing visible that would lead you to click there. The text field's visibility is on, and I even tried coding in a txtfield.visible=true and it did nothing. Any help would be appreciated!
Note: The hyperlink control source is ="View All". Not sure if that matters or not.
All of the VBA code used on this form is below:
Option Compare Database
Private Sub cboFindNotice_AfterUpdate()
getSearchResults
End Sub
Private Function getSearchResults()
Dim sql As String
Dim errMsg As String
'== Sets value of string 'sql'
sql = "SELECT tblNotice.ID, tblNotice.noticeSEIFnoticeNumber, tblNotice.noticeJurisdiction, tblNotice.noticeDueDate, tblNotice.noticeTitle, " _
& "tblAnalysts.[analyst_lName] & "", "" & [analyst_fName] AS Expr1, tblNotice.noticeStatus" _
& " FROM tblAnalysts INNER JOIN tblNotice ON tblAnalysts.ID = tblNotice.noticeAnalyst" _
& " WHERE tblNotice.noticeAnalyst LIKE '*" & Me.cboFindNotice & "*'" _
& " ORDER BY tblNotice.noticeDueDate"
'== Displays records based on query
Me.Form.RecordSource = sql
Me.Form.Requery
End Function
Private Sub noticeSEIFnoticeNumber_Click()
DoCmd.OpenForm "frmNoticeDetails"
[Forms]![frmNoticeDetails].Controls("txtControlField").Value = Me.Controls("ID").Value
Call Forms.frmNoticeDetails.txtControlField_AfterUpdate
End Sub
Private Sub txtViewAll_Click()
Me.cboFindNotice.Value = ""
Call cboFindNotice_AfterUpdate
End Sub

Repopulating subform Recordset prompts for query parameters

I have a Continuous subform that filters data based on a series of queries. I then need to populate a subform that I have placed in the footer based on some data, and I have crafted a query to accomplish this.
Private Sub UpdateXXXXX_Info()
If (Me.FormFooter.Visible <> True) Then
Me.FormFooter.Visible = True
End If
MsgBox "query start"
LabelXXXXX_.Caption = "XXXXX for: " & Me.[XXXXX__NAME] & " " & Me.[XXXXX__NAME] & " " & Me.[XXXXX__CATEGORY_NAME]
With CurrentDb.QueryDefs("XXXXX_Query")
.Parameters("XXXXXParam") = Me.[XXXXX_NAME]
.Parameters("XXXXXCategoryParam") = Me.[XXXXX_CATEGORY_NAME]
Set Me.XXXXX__Subform.Form.Recordset = .OpenRecordset
.Close
MsgBox "query complete"
End With
End Sub
Which gets called from Click events tied to each of the controls on the continuous form. This works great, and filters as I expect.
Due to the nature of the continuous form (which itself is a subform of another form), the entire continuous form may need to be refreshed:
Private Sub Form_Current()
Me.FormFooter.Visible = False
End Sub
Which is causing an issue when refreshing the data. It works as-expected the first time around, however clicking on another record in the top continuous form causes the subform to prompt for query parameters. Based on testing, it is doing this when setting Me.FormFooter.Visible = True; although not setting Visible = False in the Current event circumvents this (which produces other, undesirable behavior).
Access will not let me .Close the Recordset, or otherwise set it to a blank or null value. I also cannot find any way to set the parameters for a Requery. The amount of data being presented also prohibits me from pulling the full table and applying a filter instead.
How can I prevent this behavior?
Edit: For clarification, I must use stored queries as a business requirement.
Use an SQL statement to (re)create the recordsource of the subform instead.
SQL = "SELECT * FROM xQuery WHERE xParam = '" & me!Name & "'" AND " & _
"xCategoryParam = '" & me!CategoryName & """
me!subform.form.recordsource = SQL
This eliminates any requirements to open/close the recordset, and avoids having to change your original query definition. (i.e. create your parameterless query, and the WHERE command in the SQL does the heavy lifting for you.
Aha!
geeFlo's answer pointed me to the RecordSource property of the form. Playing around with that, I was able to come up with the following:
Private Sub ClearXXXXXInfo()
If Me.FormFooter.Visible = True Then
Me.XXXXX_Subform.Form.RecordSource = ""
Me.FormFooter.Visible = False
End If
End Sub
Private Sub Form_Current()
ClearXXXXXInfo
End Sub

How is this returning a blank value?

So this code is meant to serve as a simple search system to go to certain records in a recordset. I originally had it so they had to click the btnGoToID button in order to perform the search. I decided to just make it a little more user friendly and make it so the search field listened for the Enter button and that would perform the search as well.
The issue that I am running into when the code gets to strID = Trim(Nz(Me.txtSearch.Value, "")) the value will randomly come back as an empty string even though visually I can see that there is a value in the textbox.
I haven't been able to narrow down any pattern for when this issue occurs. At this point I don't really even know how to troubleshoot this problem, nor the words to search for that could yield any results in Google. All I can say is that it SEEMS like changing between records will affect whether or not the search goes through.
This has always worked up until I put in the txtSearch_KeyPress sub procedure.
'============================================================================
' txtSearch_KeyPress
'============================================================================
Private Sub txtSearch_KeyPress(KeyAscii As Integer)
'If user pressed enter
If KeyAscii = 13 Then
Call btnGoToID_Click
End If
End Sub
'============================================================================
' btnGoToID_Click
'============================================================================
' <<Purpose>>
' Allow the user to search for a specific ID
'============================================================================
Private Sub btnGoToID_Click()
On Error GoTo Err_Handler
Dim rs As Recordset
Dim strID As String
Set rs = Me.RecordsetClone
strID = Trim(Nz(Me.txtSearch.Value, ""))
If (strID <> "") Then
'Go to the ID
rs.FindFirst "ID = '" & strID & "'"
If rs.NoMatch Then
MsgBox "ID does not exist"
Else
'If we have a match, set the record as the current record
Me.Bookmark = rs.Bookmark
End If
Else
MsgBox "Please enter a valid ID.", vbOKOnly, "Invalid ID"
End If
Exit_Handler:
On Error Resume Next
Me.txtSearch.Value = ""
rs.Close
Set rs = Nothing
Exit Sub
Err_Handler:
Call LogError(Err.Number, Err.Description, "txtSearch on " & Me.Name)
Resume Exit_Handler
End Sub
After the conversation in the comments I have narrowed down this question to be a whole lot simpler. This yields an "Invalid use of null" error message even after there are several characters in the text field. Why would that happen, and what can I do to make it pick up the values in the textbox?
'============================================================================
' txtUnitNoToSearch_KeyPress
'============================================================================
Private Sub txtUnitNoToSearch_KeyPress(KeyAscii As Integer)
MsgBox Me.txtUnitNoToSearch
End Sub
I think your problem is related to the fact that a text box has 2 properties, .Value and .Text, which appear similar but behave differently.
While you have an edit in progress in the text box, the changing content is available via the .Text property. However, the content of the .Value property has not yet been updated to match.
After the text box's After Update event, .Value will contain the new content. And if focus has moved away from the text box, its .Text property will no longer even be available.
Sorry, I couldn't think how to explain that better. But I think the situation will be clearer with this KeyPress event procedure:
Private Sub txtUnitNoToSearch_KeyPress(KeyAscii As Integer)
Debug.Print "Text: '" & Me.txtUnitNoToSearch.Text & "'"
Debug.Print "Value: '" & Me.txtUnitNoToSearch.Value & "'"
End Sub
Watch the output from Debug.Print in the Immediate window as you make changes to the context of the text box. (Ctrl+g will open the Immediate window.)
One final point is that just Me.txtUnitNoToSearch gets you its default property which is .Value. Keep that in mind as you work through the rest of your code.

"Not a valid bookmark" with DAO Recordset

I'm in the process of converting an Access Data Project (ADP) into a standard ACCDB format with ODBC linked tables. In the ADP, I had overridden the Refresh button to return the user to the current record by using the following code:
Public Sub RibbonCmd_RefreshScreen(ctl As IRibbonControl, ByRef cancelDefault)
On Error GoTo ErrHandler
cancelDefault = False
DoCmd.Echo False
Dim saveBookmark
With Screen.ActiveForm
saveBookmark = .Bookmark
.Requery
.Bookmark = saveBookmark
End With
'Success - cancel the default behavior
cancelDefault = True
ExitHandler:
DoCmd.Echo True
Exit Sub
ErrHandler:
cancelDefault = False
Resume ExitHandler
End Sub
My understanding is that this should work just fine with DAO, but I get error 3159, Not a valid bookmark. I've also tried replacing .Bookmark with .Recordset.Bookmark, but that gave me the same result. Is there something I'm doing wrong here?
Actually, a requery of a form or a requery of a recordset will re-set and invalidate book marks.
So such book marks are no longer valid after a requery.
So the best approach here will depend on either
a) I simply want to re-display any changed records (and not move off current record).
b) I simply want to re-display any changed records AND ALSO display new records (the new records is the critical part).
If you just need a refresh, then you can use the appropriately called command refresh.
Eg:
Me.Refresh
Or in your case
Screen.ActiveForm.Refresh
So the above is ONE line of code and is ALL you need. The current record pointer for the form does NOT change when you use this command. All and any record changed will re-display for you.
Note that since you can behind the form button use:
Me.Refresh
Then LITTLE need is required to call a general routine as you have written.
However, if you need the form to "load" or display any new records added, then you DO have to use requery. In this case as noted book marks in this case all become invalid.
So, for code to requery, then we use the PK value (and hopefully you used the default pk of ID that been the default for 20 years). The code would then become:
Dim lngID As Long
If IsNull(Me!ID) Then Exit Sub
lngID = Me!ID
Me.Requery
Me.Recordset.FindFirst "id = " & lngID
Now of course if the PK id is not the same for each form, then you most certainly could pass the NAME of the PK value to your "general" refresh routine. It would look like:
Public Sub MyRefresh(strPK As String)
Dim lngID As Long
If IsNull(Me(strPK)) Then Exit Sub
lngID = Me(strPK)
Me.Requery
Me.Recordset.FindFirst strPK & " = " & lngID
End Sub
The "hope" here is you actually really JUST need refresh, since as noted this is only one line of code, and better yet it does NOT move the record pointer.
I use VB6 and Visual Data Manager in development. I have had the same problem. Most probably it arose when 2 users tried to update the same record in the same time. So some fields in the table are corrupted.
Here are the steps I used to solve the problem:
1- Copy the structure of the table (lets call it table1)to another table (lets call it table2).
2- Find the correpted record(s) in table1.
3- Transfer the data from table1 to table2 except the corrupted record(s)
4- Reenter the excluded record(s) to table2 again.
5- Rename table1 table3
6- Rename table2 table1
That's all folk
abdobox#yahoo.com
I have used the forms Recordset.AbsolutePosition, and this works fine e.g. in the OnKeyDown exit of a field
Dim PrefilterPosition As Long
Private Sub ValnSubject_KeyDown(KeyCode As Integer, Shift As Integer)
' Not F2 - exit
If KeyCode <> vbKeyF2 Then Exit Sub
' Get the active control
Dim ActiveCtl As Control
Set ActiveCtl = Me.ActiveControl
ActiveControlName = ActiveCtl.Name
' Is the form's filter set?
If Me.Filter = "" Then
' NO: Apply the new filter
' Note the current position in the recordset
PrefilterPosition = Me.Recordset.AbsolutePosition
' Set the filter to the Active control's value
Me.Filter = "[" & ActiveCtl.ControlSource & "]='" & ActiveCtl.Value & "'"
Me.FilterOn = Me.Filter <> ""
Me.Requery
Else
' YES: Clear the filter
Me.Filter = ""
Me.FilterOn = Me.Filter <> ""
Me.Requery
' Align the recordset on the previously stored position
Me.Recordset.AbsolutePosition = PrefilterPosition
End If
' Restore the cursor to where it came from
Me.Controls(ActiveControlName).SetFocus
Ex_it:
End Sub
For context: this code was from an idea for an 'Instant Filter', where you position the cursor on a field in a tab form, press F2, and then a filter is applied so you see only records with the selected field's value. Press F2 again and the filter is removed and the cursor goes back into the place it was when you hit F2 the first time. Bookmarks do not work here, as Albert says above.