20 December 2006 by Ross McLean
Here’s something i did not know about VBA!
http://www.puremis.net/excel/cgi-bin/yabb/YaBB.pl?num=1166424720/0#8
Cool, now i just have to get a job at the Natural History Museum.
Tags: Examples, VBA
Categories: Code •
1 Comment »
14 August 2006 by Ross McLean
Ran in to an old friend today, and it took me a while to remember what to do. After adding a list box to your user form, you’ll want to resize it so that it looks nice. At design time you can get all your controls lined up without too much effort. Like this:
However when the form is run, the list box automatically resizes itself! Like this!
The answer is to change the a default setting, the “IntegralHeight”, and problem solved. The difference is that now your list box will show parts of lines that don’t fill the list box exactly, rather than resizing to accommodate only whole lines.
Tags: Examples, Forms, VBA
Categories: Code •
2 Comments »
8 August 2006 by Ross McLean
The situation. Â
Back end Access DB, front ended with an Excel Form. Add a new record to a table in the DB and run a Make Table Query to sort the whole table in to alphabetical order. Reload the table into a list box.
I need to make a new table because simply sorting the existing table does not results in a alphabetica import into the list box.
The problem is that when I run the SQL for the update query, if the table name is the same as a table already in the DB then DAO errors. In Access you have to click a message box to allow the deletion of the old table.
From VBA I can use DAO with "TableDefs.Delete" to remove the old table then the solution works fine. But I don't really like this idea, it is clunky and I don't like deleting table danger!
So what's a better way?
The workhorse code looks like this:
VBA:
-
Sub SendNewProject(sPro As String, sDes As String)
-
-
   Dim i As Integer
-
   Dim db As Database, rs As Recordset, r As Long
-
   Dim sSql As String
-
-
   Set db = OpenDatabase(gsDataBase)
-
   Set rs = db.OpenRecordset("tblProject", dbOpenTable)
-
-
   With rs
-
       .AddNew
-
       .Fields("Project") = sPro
-
       .Fields("Description") = sDes
-
       .Update
-
   End With
-
-
   '''close table we will delete shortly
-
   rs.Close
-
   Set rs = Nothing
-
-
   '''run updatequery to update project list
-
   sSql = " SELECT tblProject.Project INTO tblProjectS" _
-
          & " FROM tblProject" _
-
          & " ORDER BY tblProject.Project;"
-
-
   '''delete old projectsS table
-
   db.TableDefs.Delete "tblProjectS"
-
   '''run make new table
-
   db.Execute sSql
-
-
   '''close DB
-
   db.Close
-
   Set db = Nothing
-
-
   MsgBox "Project has been added to the projects list" _
-
          & vbNewLine _
-
          & vbNewLine _
-
          , vbOKOnly, gcsMsgboxhead
-
-
End Sub
Tags: Examples, VBA
Categories: Code •
No Comments »
3 August 2006 by Ross McLean
Most of us know that .dll's are the only real option if security is a genuine issue, but I think the following might be a possible (bodge) solutions for those who many not have VB, and need better security for their VBA code. Anyway, I was looking at this site:
Tech Net - Office 2000/Visual Basic Programmer's Guide
Chapter 17 deals with security. My lighting fast brain crawled in to action and I thought I'd try something.
Now according to this info:
In Access, you can save an .mdb or .adp file as a file type that contains only compiled VBA code without the source code.
These files are called .mde file and most of us will have worked with these before, or at least heard of them. I didn't know the code was compiled though. Doing a little bit of goggling it seems that decompiling is available, but that the VBA code remains compiled, i.e. forms and report are easy to decompile, but the VBA remains compiled good news.
See where we are heading? You can call this .mde code from you Excel/Word etc VBA, and your more sensitive VBA code can be kept in a complied- harder to get at - .mde file. In theory it's a bit like putting your code in a proper .dll it's compiled code. Which kinda begs the question, why can you do this with all our VBA?
Here is an example I've put it into a UDF, and it runs in such a way as to be dog slow, but it could be implemented better, or if the situations was different the performance hit would be less noticeable I'm sure you'll get the idea. I've inculed the orginal mdb, and you can do all the other passwording etc to this mde before making it a mde. I've also chnaged the file extention to change the icon, whcih is of little importance.
So, it's Heath Robison, it's far from ideal but it might be useful to someone; oneday! A nice little idea I though"¦Â
Tags: Downloads, Examples, Speed, VBA
Categories: Code, Downloads, General •
3 Comments »
18 June 2006 by Ross McLean
Obviously you could adapter it to do what ever type of text file you like (well, and Excel can handle)
VBA:
-
Option Explicit
-
-
Private Const BIF_RETURNONLYFSDIRS As Long = &H1
-
Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
-
Private Const BIF_RETURNFSANCESTORS As Long = &H8
-
Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000
-
Private Const BIF_BROWSEFORPRINTER As Long = &H2000
-
Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000
-
Private Const MAX_PATH As Long = 260
-
-
Type BrowseInfo
-
hOwner As Long
-
pidlRoot As Long
-
pszDisplayName As String
-
lpszINSTRUCTIONS As String
-
ulFlags As Long
-
lpfn As Long
-
lParam As Long
-
iImage As Long
-
End Type
-
-
Type SHFILEOPSTRUCT
-
hwnd As Long
-
wFunc As Long
-
pFrom As String
-
pTo As String
-
fFlags As Integer
-
fAnyOperationsAborted As Boolean
-
hNameMappings As Long
-
lpszProgressTitle As String
-
End Type
-
-
Declare Function SHGetPathFromIDListA Lib "shell32.dll" ( _
-
ByVal pidl As Long, _
-
ByVal pszBuffer As String) As Long
-
-
Declare Function SHBrowseForFolderA Lib "shell32.dll" ( _
-
lpBrowseInfo As BrowseInfo) As Long
-
-
Public Sub Save_To_Where()
-
-
Dim sFolderName As String
-
Dim sFileName As String
-
Dim sdefult As String
-
-
sdefult = "M.I.E Export to CSV - " & Application.UserName & " - " _
-
& Round((Timer), 0)
-
-
sFolderName = BrowseFolder("Save Text File Where?")
-
If sFolderName = "" Then
-
Exit Sub
-
Else
-
sFileName = InputBox("Entre the file name you would like to use", "File Name", sdefult)
-
If Len(sFileName) = 0 Then
-
Exit Sub
-
End If
-
End If
-
-
SaveAsText sFolderName, sFileName
-
-
End Sub
-
-
'''Fuction to get directory
-
Function BrowseFolder(Optional Caption As String = "") As String
-
-
Dim BrowseInfo As BrowseInfo
-
Dim FolderName As String
-
Dim ID As Long
-
Dim Res As Long
-
-
With BrowseInfo
-
.hOwner = 0
-
.pidlRoot = 0
-
.pszDisplayName = String$(MAX_PATH, vbNullChar)
-
.lpszINSTRUCTIONS = Caption
-
.ulFlags = BIF_RETURNONLYFSDIRS
-
.lpfn = 0
-
End With
-
-
FolderName = String$(MAX_PATH, vbNullChar)
-
-
ID = SHBrowseForFolderA(BrowseInfo)
-
-
If ID Then
-
Res = SHGetPathFromIDListA(ID, FolderName)
-
If Res Then
-
BrowseFolder = Left$(FolderName, InStr(FolderName, _
-
vbNullChar) - 1)
-
End If
-
End If
-
-
End Function
-
-
Public Sub SaveAsText(sFolder As String, sName As String)
-
-
On Error GoTo ErrorHandler
-
-
Application.ScreenUpdating = False
-
-
Selection.Copy
-
-
Workbooks.Add
-
ActiveWorkbook.Sheets(1).Paste
-
ActiveWorkbook.SaveAs Filename:=sFolder & "\" & sName & ".csv", FileFormat:=xlCSVMSDOS
-
Application.DisplayAlerts = False
-
ActiveWorkbook.Close
-
Application.DisplayAlerts = True
-
Application.ScreenUpdating = True
-
-
Exit Sub
-
-
'''Error handerling
-
ErrorHandler:
-
If Err.Number = 1003 Then
-
MsgBox "Error, did you use <,>,?,[,], :, | or *" _
-
& vbNewLine & "Make sure the folder exists" _
-
& vbNewLine & "Make sure the ile/path name is not onger than 218 letters" _
-
& vbNewLine & "Make sure the folder is not read only", _
-
vbOKOnly, "Error Exporting Text File"
-
Exit Sub
-
Else
-
MsgBox "An unexpected error occured, export aborted", vbOKOnly, "Error Exporting Text File"
-
Exit Sub
-
End If
-
-
End Sub
Tags: Data, Examples, VBA
Categories: Code •
No Comments »