The Ten Commandments of Access

by MYLE 17. March 2011 06:54

  1. Make sure you start with a properly-normalized database design and that you understand all of the fields and relationships before beginning to code. 
  2. Don't allow users to see or edit tables directly, but only through forms. Don't use "Lookup Fields" (at least, not on a table level) either, as they can lead to confusion. 
  3. Choose a naming convention and use it. Never allow spaces in names. 
  4. Write comments in your procedures and explain each variable. Do this as you code: don't rely on going back "once the program's done". 
  5. Understand error handling and use it faithfully in all procedures. 
  6. Split your databases into a front-end and back-end. 
  7. Don't use Autonumber if the field is meant to have meaning for the users. 
  8. Don't copy and paste other people's code without at least attempting to understand what it does. 
  9. Don't use "SendKeys", "Smart Codes" or "GoTo" (unless the GoTo is part of an OnError process) 
  10. Back-up the database faithfully. Never make changes on the Production Database, but on the Prototype Copy instead. 

Status Bar in MSaccess

by MYLE 16. July 2010 00:09

Here some code I found that will let you change the status bar in MSaccess

Function StatusBar(Message)
 Dim RetVal As Variant
 RetVal = SysCmd(4, Message)
End Function
Call StatusBar("Hello There")

Microsoft SysCmd

Change the printer in MSaccess

by MYLE 16. July 2010 00:01

Every wanted to be able let the User Change the printer in MSaccess

Dim prt As Printer
For Each prt In Application.Printers
    Me!Myprinter.AddItem Item:=prt.DeviceName
Next prt

where Me!Myprinter is a combo box on the form Now Printing that report

Dim prt
Set prt = Application.Printers(Me!Myprinter)
Set Application.Printer = prt
stDocName = "That Report"
DoCmd.OpenReport stDocName, acNormal   
Set Application.Printer = Nothing

Display Time format

by MYLE 21. June 2010 21:49

I have been ask many time what the best way to store hours in a data base.
as hour is a mod 60 what I do store MINS and use the below function to convert the mins to time format so
if a job took me 1:30 I store the min value 90min then if a did and other hour store that as 60 then add the mins up 150min total pass the

total = Mintohrs(150)

total would show 02:30 HrstoMin

Function Mintohrs(Tmin)
    hh = Int(Tmin / 60)
    tt = hh * 60
    mm = Tmin - tt
    Mintohrs = DIG(hh) & ":" & DIG(mm)
End Function

hourtomin

 my other Functions

03:15 = Mintohrs(90+90+15)

I use and other Function I wrote to format the Mins

Want 2 dig format

by MYLE 21. June 2010 21:44
Here
Function DIG(num)
     if len(num)<>2 then
        DIG ="0" & num 
     else
        DIG = num 
     end if
End Function

Age return

by MYLE 21. June 2010 21:39

some ask me need to know the age of someone to the Month ie 45.5 months don't know why but ....

Function Age(Birth_Date,End_Date)
'***************************************
' Works out the age of to the month
'
'***************************************
Dim Months
Dim Years
Dim Temp
If IsNull(Birth_Date) or Birth_Date ="" Then
    Age=0.0 
else
    Months = DateDiff("m", CDate(Birth_Date), End_Date)
    Years = Int(Months / 12)
    Temp = Years * 12
    If Years = 0 then Years = "" 
    Age =  Years & "." & Months - Temp
End if
End Function
MyAge = age(DOB,now())

Find the 1st day of a month / Last day of month

by MYLE 21. June 2010 21:23

I had to Find the Last day on the Month that the hard bit but got thinking why note just find the first day on the next month then -1 day form that so here it is

 

Function st1month(Tdate)
    st1month = DateSerial(Year(Tdate), Month(Tdate) + 1, 1)
End Function

OK now let find the Last day on month

Function LastDay(Tdate)
    Lastd = dateadd("m",1,st1month(Tdate))
    LastDay = DateAdd("d", -1, Lastd)
End Function

Must have both function in your code.

some CreateObject("Scripting.FileSystemObject")

by MYLE 18. June 2010 21:16

Some handy function

Function ReportFolderStatus(fldr) As Boolean
   Dim fso, msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   If (fso.FolderExists(fldr)) Then
      ReportFolderStatus = True
   Else
      ReportFolderStatus = False
   End If
   Set fso = Nothing
End Function

Function CreateFolder(FolderName)
On Error Resume Next
   Dim fso, f
   If Not ReportFolderStatus(FolderName) Then
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.CreateFolder(FolderName)
   End If
   Set fso = Nothing
End Function

Sub SaveAction(FullPathFile, FeildName, strEntry, Deleteit)
    'write the data to a log file
    If Deleteit And ReportFileStatus(FullPathFile) Then Kill FullPathFile
    Dim strErrMsg, f, fsoLog
    Set fsoLog = CreateObject("Scripting.FileSystemObject")
    Set f = fsoLog.OpenTextFile(FullPathFile, 8, True, -2)
    Outline = FeildName & ":" & strEntry
    f.WriteLine Outline
    f.Close
    Set fsoLog = Nothing
End Sub

Function ReportFileStatus(filespec)
   Dim fso, msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   If (fso.FileExists(filespec)) Then
      ReportFileStatus = True
      Exit Function
   Else
      ReportFileStatus = False
      Exit Function
   End If
   Set fso = Nothing
End Function

A quick update to a table

by MYLE 18. June 2010 21:04

base on the dlookup function in MSaccess

Sub UpLookup(Feild, FeildValue, Table, Crit)
    Dim SQL As String
    Dim DB As Database
    Dim rs As Recordset
    Set DB = CurrentDb
    SQL = ""
    SQL = " SELECT " & Table & ".*"
    SQL = SQL & " FROM " & Table & ""
    SQL = SQL & " WHERE " & Crit
    Set rs = DB.OpenRecordset(SQL)
    If Not rs.EOF Then
        rs.Edit
        rs.Fields(Feild) = FeildValue
        rs.Update
    End If
    rs.Close
End Sub

Weekending Date

by MYLE 10. June 2010 05:13

I first wrote this function in lotus 123

off top of heap going back 20+ year  +iif((a1 mod 7)>0,a1-(a1 MOD7)+7,a1)) please don't quote me on this

this would of been one of my first function I wrote

 Public Function WEEKEND(dat) As Date
    If IsNull(dat) Then Exit Function
    dat = DateSerial(Year(dat), Month(dat), Day(dat))
    If dat Mod 7 > 0 Then
    WEEKEND = dat - dat Mod 7 + 7
    Else
    WEEKEND = dat
    End If
 End Function

I know the Above Function works in VB 

Work in 

excel

Msaccess

 

Dislexia

I see people have commented about my style of writting.
Well I have dislexia so spelling is not one of my good points
but when it comes to reading/written code thats the easy bit.

 

 

Poll

What Search Engine Do you use




Show Results

Disclaimer of Liability

While every effort will be made to ensure that the information contained within this website is accurate and up to date, The Company's listed with this web site and any associated parties, make no warranty, representation or undertaking, whether expressed or implied, nor does it assume any legal liability, whether direct or indirect, or responsibility for the accuracy, completeness, or usefulness of any information.

 

Google Ads

Most comments

carpet cleaning forest lake carpet cleaning forest lake
1 comments
us United States
carpet cleaning capalaba carpet cleaning capalaba
1 comments
us United States

RecentComments

Comment RSS