Documente Academic
Documente Profesional
Documente Cultură
Have you ever wanted to requery a form after a user inserts a new record or modifies an
existing record, to perhaps re-order things, but wanted to stay on the same record that you
currently are on once the requery was done?
The fact of the matter is that it truly isnt very complex to do. Below is some straight forward
code to do so and youd need only add it to a Forms After Insert event or a controls After
Update event.
Dim rs As DAO.Recordset
Dim pk As Long
pk = Me.PrimaryKeyFieldName
Me.Requery
Set rs = Me.RecordsetClone
rs.FindFirst "[PrimaryKeyFieldName]=" & pk
Me.Bookmark = rs.Bookmark
Set rs = Nothing
Now there is nothing wrong with the code above, but instead of putting such code inside each and
every forms After Insert event and every controls After Update event, I thought to myself that I
should be able to create a simple, re-useable function that I could call, and achieve the same desired
effect. Below is that function.
'--------------------------------------------------------------------------
-------------
' Procedure : FrmRequery
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Requery the form to apply the chosen ordering,
' but ensure we remain on the current record after the
requery
' Copyright : The following may be altered and reused as you wish so long
as the
' copyright notice is left unchanged (including Author, Website
and
' Copyright). It may not be sold/resold or reposted on other
sites (links
' back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' frm : The form to requery
' sPkField : The primary key field of that form
'
' Usage:
' ~~~~~~
' Call FrmRequery(Me, "Id")
' Call FrmRequery(Me, "ContactId")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
'
***************************************************************************
***********
' 1 2012-Oct-19 Initial Release
'--------------------------------------------------------------------------
-------------
Sub FrmRequery(frm As Form, sPkField As String)
On Error GoTo Error_Handler
Dim rs As DAO.Recordset
Dim pk As Long
pk = frm(sPkField)
frm.Requery
Set rs = frm.RecordsetClone
rs.FindFirst "[" & sPkField & "]=" & pk
frm.Bookmark = rs.Bookmark
Error_Handler_Exit:
On Error Resume Next
Set rs = Nothing
Exit Sub
Error_Handler:
MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: FrmRequery" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub
The beauty of the above function is that you can copy it into a standard module, and then call it with
a single line of code in as many events as you choose. You could even build an event expression, thus
not requiring any VBA events, if you so wished to.
VBA VBE Enumerate Modules,
Procedures and Line Count
It still amazes me how Microsoft can develop these complexe applications but seems to
overlook some simple functions that they should included within them to aid developers
But then, as demonstrated with the release of Office 2007 and 2010, Microsoft is not
interested in the developer, they are only interested in the end-users opinion. Not productivity
(that went down, about 30-40% drop in efficiency, the tubes with their change of format)! So
all that matters is looks, the feel very superficial (rant over)!!!
This will be the first in a series of procedure that I will be posting in the coming months in
which I hope to demonstrate how you can use the Microsoft Visual Basic for Application
Extensibility library in conjuntion with the power of VBA to learn more, control more,
manipulate more the VBE.
In this first post, I simply wanted to create a simple procedure that would give me a
breakdown of my Access project. I wanted to return a listing of procedure per module with a
line count. As you can see, the Microsoft Visual Basic for Application Extensibility enable
us to perform this task with ease with little code. Heck, half of the code below is to write to
the generated text file!
'--------------------------------------------------------------------------
-------------
' Procedure : GetVBEDeatils
' Author : CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Goes throught the VBE and creates a text file which give a
brief listing
' of the procedures within each module and a line count for
each
' Copyright : The following may be altered and reused as you wish so long
as the
' copyright notice is left unchanged (including Author, Website
and
' Copyright). It may not be sold/resold or reposted on other
sites (links
' back to this site are allowed).
' Requirements: reference to the Microsoft Visual Basic for Application
Extensibility
' library.
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
'
***************************************************************************
***********
' 1 2011-June-04 Initial Release
'--------------------------------------------------------------------------
-------------
Function GetVBEDeatils()
Dim vbProj As VBProject
Dim vbComp As VBComponent
Dim vbMod As CodeModule
Dim sProcName As String
Dim pk As vbext_ProcKind
Dim FileNumber As Integer
Dim strFile As String
Const vbNormalFocus = 1
The procedure has 2 input variables: strPath which is the full path of the directory whose files
you wish to import all the file paths from adn strFilter which is an optional input variable
should you wish to refine what type of document is imported (for instance is you only want to
import PDFs then youd enter pdf, Word documents doc, and so on).
Set db = CurrentDb()
'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$(strPath & "*." & strFilter)
Do While MyFile <> ""
'Debug.Print MyFile
sSQL = "INSERT INTO [YourTableName] (YourTableFieldName) VALUES(""" &
MyFile & """)"
db.Execute sSQL, dbFailOnError
'dbs.RecordsAffected 'could be used to validate that the
'query actually worked
MyFile = Dir$
Loop
Error_Handler_Exit:
On Error Resume Next
Set db = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: ImportDirListing" & vbCrLf & _
"Error Description: " & Err.Description, vbCritical, _
"An Error has Occured!"
Resume Error_Handler_Exit
End Function