Archive

Archive for the ‘MS Access’ Category

Change MS Access application title and icon using VBA

December 15th, 2009 No comments

Here is a code to change MS Access application title and icon using VBA. This code also sets the application icon as form and report default icon.

Dim db As Database
Set db = CurrentDb
db.Properties("AppIcon").Value = CurrentProject.Path & "\Bee.ico"
db.Properties("AppTitle").Value = msgMainTitle
db.Properties("UseAppIconForFrmRpt").Value = True
Application.RefreshTitleBar

Click here for list of all properties exposed by CurrentDB.Properties().

Categories: MS Access Tags: , ,

List of MS Access properties available through CurrentDB.Properties

December 15th, 2009 1 comment

CurrentDB.Properties is nothing but a collection. If you can iterate through a collection, you will be able to get the list of all the properties. Code to iterate through all properties is

Dim i As Integer
For i = 0 To CurrentDb.Properties.count - 1
   Debug.Print CurrentDb.Properties(i).Name
Next

It gave me following list of properties.

Name
Connect
Transactions
Updatable
CollatingOrder
QueryTimeout
Version
RecordsAffected
ReplicaID
DesignMasterID
Connection
ANSI Query Mode
Themed Form Controls
AccessVersion
Build
ProjVer
StartUpForm
StartUpShowDBWindow
StartUpShowStatusBar
AllowShortcutMenus
AllowFullMenus
AllowBuiltInToolbars
AllowToolbarChanges
AllowSpecialKeys
UseAppIconForFrmRpt
Track Name AutoCorrect Info
Perform Name AutoCorrect
AppTitle
AppIcon

I don’t know the data types of the these properties but I put it for quick reference.

Categories: MS Access Tags: ,

ADODB Command.Execute does not return error for RAISERROR

December 4th, 2008 No comments

Description:

I was executing a stored proc which would halt its execution using RAISERROR. When I tried to run the stored proc in Query Analyzer it worked fine and gave me error but in VBA it couldn’t halt the execution and continued to the next statement. Read more…

Categories: MS Access Tags: , , ,

MS Access Pass-through query Error: Cannot execute a select query

June 19th, 2008 No comments

When I tried to run an UPDATE statement from pass-through query, it throwed error “Cannot execute a select query”. This is due to that fact the property ReturnsRecords is set to True which requires the pass through query to return results. If it does not return recordset, it will throw an error. You can correct this error by

  1. Open pass-through query into design mode. Click on Properties button (Alt+Enter) and set Return Records to No
  2. Using program you can set QueryDef object’s ReturnsRecords property to False.
Categories: MS Access Tags: ,

MS Access: Check If The Normal Form Or Subform Form Is Loaded

April 18th, 2008 1 comment

If you need to check if MS Access form is loaded or not, you can easily check it using the folloing code

If CurrentProject.AllForms("FORMNAME").IsLoaded = True Then
	' DO SOMETHING
End If

This code is good until you are using single form application. This code will fail if the form is used as subform. To search all the forms including subform, I have created a function calling which will search for all the loaded forms and gives the result as boolean value. Here is the example to check if the form is loaded or not

Dim IsLoaded as Boolean
IsLoaded = IsFormLoaded("FORMNAME")

The function IsFormLoaded will search each and every control within each form to find the form FORMNAME. Of course, this is a overhead to the application so to reduce the amount of search, two optional arguments can be provided which can be very helpful in searching subform. If you already know the parent form name of the subform and the controlname which holds subform within parent form, the time to search subform will be reduced. For example

Dim IsLoaded as Boolean
IsLoaded = IsFormLoaded("FORMNAME","PARENTFORMNAME")

will search all the controls withing PARENTFORMNAME form while

Dim IsLoaded as Boolean
IsLoaded = IsFormLoaded("FORMNAME","PARENTFORMNAME","CONTROLNAME")

will search for the CONTROLNAME control within PARENTFORMNAME form to check if the FORMNAME form is loaded.

The above mentioned functions are here.

Public Function IsFormLoaded(strFormName As String,
		Optional LookupFormName As Form,
		Optional LookupControl As Control) As Boolean
    Dim frm As Form
    Dim bFound As Boolean

    If Not (IsMissing(LookupFormName) Or IsNull(LookupFormName)
		Or LookupFormName Is Nothing) Then
        If Not (IsMissing(LookupControl) Or IsNull(LookupControl)
 		Or LookupControl Is Nothing) Then
        On Error GoTo ErrorHandler:
            If LookupControl.ControlType = acSubform Then
                If LookupControl.Form.Name = strFormName Then
                    bFound = True
                End If
            End If
        Else
            Call SearchInForm(LookupFormName, strFormName, bFound)
        End If
    Else
        For Each frm In Forms
            If frm.Name = strFormName Then
                bFound = True
                Exit For
            Else
                Call SearchInForm(frm, strFormName, bFound)
                If bFound Then
                    Exit For
                End If
            End If
        Next
    End If
ErrorHandler:
    IsFormLoaded = bFound
End Function

Public Function SearchInForm(frm As Form, strDoc As String,
 			bFound As Boolean)
On Error GoTo ErrorHandler
    Dim ctl As Control

    For Each ctl In frm.Controls
        If ctl.ControlType = acSubform Then
            If ctl.Form.Name = strDoc Then
            'If ctl.SourceObject = strDoc Then
                bFound = True
            Else
                Call SearchInForm(ctl.Form, strDoc, bFound)
            End If
SkipElement:
            If bFound Then
                Exit For
            End If
        End If
    Next
    Exit FunctionErrorHandler:
            Resume SkipElement
End Function

how to assign recordset to the recordset property of listbox?

June 25th, 2006 No comments
set me.List0.Recordset = rs.cloners.close

by this way the new recordset will be assigned to the list and the original will be closed.

Categories: MS Access Tags:

Things to remember while converting Microsoft Access application into SQL Server application

May 9th, 2006 1 comment

When the Access database grows much large and the performance of the application gets worsen, you might need to think of upsizing the Access database into SQL Server database. So that you can split your MS Access front end and SQL Server back end. Microsoft Access comes with a utility called “Upsizing Wizard”. It is located at Tools > Database Utility > Upsizing Wizard. This utility tries its best to convert the whole database including data tables, queries, relationships etc. This wizard is able to convert all tables perfectly but it might convert all queries into stored procedures or functions or views.

Choose the solution between Access application with Linked tables or Access Data Project.

When you are linking a table from a database on the network, the datasource is not updateable. So if the application adds/updates/deletes record, then this solution is not for you. Access Data Project is the best solution for the application which needs updateable recordset. One can import different objects like forms/reports in Access Data Project. Here the tables are not linked but you can direclty manipulate the database.

How to convert query into stored procedure?

When the query is directly used in a form which is using/not using a form variable, convert it into stored procedure.
Ex. The query is select * from EMP where Name like [forms]![Emp Form]![Name]
Create Procedure GetEmployee
(
    @Name nvarchar(20)
)
AS
    Select * from EMP where Name like @Name

If the query is using another query without any variable, make the first query as a stored procedure and convert second query into view.

If the query is using another query with variable, make the first query as stored procedure with variable and convert second query into a function.
Now you can use this function result as a temporary table.

Use Pass-through query wherever possible, because it reduces network traffic.

and last but not least

PAY ATTENTION TO THE BOOLEAN COMPARISION AND NULL VALUE CONCATENATION

Once you upsize the database, MS wizard gives you report of how many tables successfully upsized and how it changed the datatypes of columns. Most of the time, it upsizes it correctly but sometimes it just skips some tables/records. So to check it I have created an Access form which can be run to check the consistency of the records. Open the main form “frmMain” give the appropriate value and press “Check…” button. It will store the results in database. Now you can check the results by viewing the queries.

Categories: MS Access Tags: