tag:blogger.com,1999:blog-101939502009-06-08T05:17:26.689-04:00Access Programmer - Orlando GonzalezTechnical bits and pieces about working with MS Access, Excel and SQL Server.Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.comBlogger101125tag:blogger.com,1999:blog-10193950.post-1124920214144761642009-05-03T13:34:00.011-04:002009-05-28T19:49:56.956-04:00Filter Combobox based on Another Combobox<STRONG>How to synchronize two combo boxes in a Microsoft Access Form.<BR><br /><br />Watch the video and/or follow the step by step instructions below (this will take about 15 minutes of your time but you will get what you came for!)<BR><br /><br /><object width="400" height="300" class="BLOG_video_class" id="BLOG_video-4a2ee35116932daf" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="movie" value="http://www.blogger.com/img/videoplayer.swf?videoUrl=http%3A%2F%2Fvp.video.google.com%2Fvideodownload%3Fversion%3D0%26secureurl%3DqAAAAPCZD0ddCGBZjZs6HcCGJYcsngZD74fErR25dHPMHqM-UDn7Gm6pOEi-GeUpnEHXsOmVpKctG-0RkFLKcLE6ZZHEtP_N2gfz0awBOlJEOkRGNAA9bunJmT8ze099btTZtmaiulTLjtuqQNCqgQTBXuwmqM2xMde7GLuFI9AUw7Ou5t0jfFpDmuJPGFY9P9vQoWkOmVqZMuJC4rmX2ClidbEduLTucbWB0f36d6vvg6Mj%26sigh%3DuBgNQZQVEqFRK7ZUpgRIKZ6aKS8%26begin%3D0%26len%3D86400000%26docid%3D0&amp;nogvlm=1&amp;thumbnailUrl=http%3A%2F%2Fvideo.google.com%2FThumbnailServer2%3Fapp%3Dblogger%26contentid%3D4a2ee35116932daf%26offsetms%3D5000%26itag%3Dw320%26sigh%3DpChFeFPTO6_P7etY7R8KT6Yhbwc&amp;messagesUrl=video.google.com%2FFlashUiStrings.xlb%3Fframe%3Dflashstrings%26hl%3Den"><param name="bgcolor" value="#FFFFFF"><embed width="400" height="300" src="http://www.blogger.com/img/videoplayer.swf?videoUrl=http%3A%2F%2Fvp.video.google.com%2Fvideodownload%3Fversion%3D0%26secureurl%3DqAAAAPCZD0ddCGBZjZs6HcCGJYcsngZD74fErR25dHPMHqM-UDn7Gm6pOEi-GeUpnEHXsOmVpKctG-0RkFLKcLE6ZZHEtP_N2gfz0awBOlJEOkRGNAA9bunJmT8ze099btTZtmaiulTLjtuqQNCqgQTBXuwmqM2xMde7GLuFI9AUw7Ou5t0jfFpDmuJPGFY9P9vQoWkOmVqZMuJC4rmX2ClidbEduLTucbWB0f36d6vvg6Mj%26sigh%3DuBgNQZQVEqFRK7ZUpgRIKZ6aKS8%26begin%3D0%26len%3D86400000%26docid%3D0&amp;nogvlm=1&amp;thumbnailUrl=http%3A%2F%2Fvideo.google.com%2FThumbnailServer2%3Fapp%3Dblogger%26contentid%3D4a2ee35116932daf%26offsetms%3D5000%26itag%3Dw320%26sigh%3DpChFeFPTO6_P7etY7R8KT6Yhbwc&amp;messagesUrl=video.google.com%2FFlashUiStrings.xlb%3Fframe%3Dflashstrings%26hl%3Den" type="application/x-shockwave-flash"></embed></object><br /><BR><br /><br /></STRONG><U><STRONG>Requirements</STRONG></U> <br /><UL><br /><LI>This example uses MS Access 2007 but it should also work in previous versions. <br /><LI>You will need six objects in your database: </LI><br /><UL><br /><LI>One form: frmMain <br /><LI>Two combo boxes: The Master combo box: cboProjects and The Slave combo box: cboProjectTasks <br /><LI>One table: tblProjectsAndTasks <br /><LI>Two queries: The Master query: qryProjects and The Slave query: qryProjectTasks </LI></UL></UL><STRONG><U>STEP 1: Create the Form: </U></STRONG><br /><UL><br /><LI>Create a new form and name it frmMain. <br /><LI>Save the form. </LI></UL><STRONG><U>STEP 2: Create the combo boxes: </U></STRONG><br /><UL><br /><LI>Create the Master combo box. <br /><LI>Name it: cboProjects <br /><LI>Create the Slave combo box. <br /><LI>Name it: cboProjectTasks <br /><LI>Save the form. </LI></UL><STRONG><U>STEP 3: Create the data table: </U></STRONG><br /><UL><br /><LI>Create a table and name it: tblProjectsAndTasks <br /><LI>Add these FieldNames (and DataTypes): </LI><br /><UL><br /><LI>TaskID (Autonumber) <br /><LI>ProjectName (Text) <br /><LI>TaskName (Text) </LI></UL><br /><LI>Populate the table. </LI><br /><UL><br /><LI>In this example the projects are: Alpha, Delta and Gamma. <br /><LI>And each project has multiple tasks, for example: </LI><br /><UL><br /><LI>Project Alpha Task 1 <br /><LI>Project Alpha Task 2 <br /><LI>Project Gamma Task 3 </LI></UL></UL></UL><U><STRONG>STEP 4: Create the master query: </STRONG></U><br /><UL><br /><LI>The Master Query should be named: qryProjects <br /><LI>This query will select unique project names from tblProjectsAndTasks. <br /><LI>It will be the data source for the master combo box. <br /><LI>The SQL code for this query is: <SPAN style="COLOR: #3366ff">SELECT DISTINCT ProjectName FROM tblProjectsAndTasks;</SPAN> <br /><LI>Alternatively, you could use the following SQL code: <SPAN style="COLOR: #3333ff">SELECT ProjectName FROM tblProjectsAndTasks GROUP BY ProjectName;</SPAN> <br /><LI>Either way, the output should be: <SPAN style="COLOR: #33cc00"></LI><br /><UL><br /><LI>Project Alpha <br /><LI>Project Delta <br /><LI>Project Gamma </LI></UL></UL></SPAN><STRONG><U>STEP 5: Create the Slave Query: </U></STRONG><br /><UL><br /><LI>Create a new query and name it: qryProjectTasks <br /><LI>This query will show tasks related to the project that is selected in cboProjects. <br /><LI>The SQL code for this query is: <SPAN style="COLOR: #3333ff">SELECT DISTINCT ProjectName, TaskName FROM tblProjectsAndTasks WHERE ProjectName =[forms]![frmMain].[cboProjects].[value]; </SPAN><br /><LI>At this point, running this query will not return any results because the WHERE clause is looking for the project selected in cboProjects, which is not data bound yet! </LI></UL><STRONG><U>STEP 6: Data Binding the Master Combo Box: </U></STRONG><br /><UL><br /><LI>Give cboProjects these properties: </LI><br /><UL><br /><LI>Row Source: qryProjects <br /><LI>Row Source Type: Table/Query <br /><LI>Column Count: 1 <br /><LI>Bound Column: 1 </LI></UL></UL><STRONG><U>STEP 7: Data Binding the Slave Combo Box: </U></STRONG><br /><UL><br /><LI>Give cboProjectTasks these properties: </LI><br /><UL><br /><LI>Row Source: qryProjectTasks <br /><LI>Row Source Type: Table/Query <br /><LI>Column Count: 2 <br /><LI>Column Width: 0";1" <br /><LI>Bound Column: 2 </LI></UL></UL><STRONG><U>STEP 8: Refreshing the Slave Combo Box: </U></STRONG><br /><UL><br /><LI>The slave combo box needs to be refreshed after each change in the master combo box. The VBA procedure below will refresh the slave combo box. <br /><LI>This procedure should be tied to the On Change event of the master combo box. <SPAN style="COLOR: #3333ff">Private Sub cboProjects_Change() Me.cboProjectTasks.Requery End Sub </SPAN></LI></UL><br /><P><STRONG><U>STEP 9: Clearing the Slave Combo Box:</U></STRONG> </P><br /><UL><br /><LI>Refreshing the combo box does not clear the current task selected. </LI></UL><br /><P>Modify the VBA procedure to clear the combo box:<br /><SPAN style="COLOR: #3333ff"><br />Private Sub cboProjects_Change()<BR><br />Me.cboProjectTasks.Requery<BR><br /><STRONG>Me.cboProjectTasks = ""</STRONG><BR><br />End Sub </SPAN><BR><br /><br /><STRONG><U>STEP 10: ENJOY!</U></STRONG> </P><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112492021414476164?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com14tag:blogger.com,1999:blog-10193950.post-33419630676454410132008-02-28T13:11:00.001-05:002008-02-28T13:17:37.951-05:00SQL APPEND TABLE vs. CREATE TABLE<span style="color:#009900;">--TO <strong>CREATE A NEW TABLE:</strong></span><strong><br /></strong><br /><span style="color:#000099;">SELECT</span> *<br /><span style="color:#000099;">INTO</span> newTableName<br /><span style="color:#000099;">FROM</span> sourceTableName<br /><br /><br /><br /><span style="color:#009900;">--TO <strong>APPEND DATA TO AN EXISTING TABLE</strong>:</span><br /><br /><span style="color:#000099;">INSERT INTO</span> existingTableName<br /><span style="color:#000099;">SELECT</span> *<br /><span style="color:#000099;">FROM</span> sourceTableName<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-3341963067645441013?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0tag:blogger.com,1999:blog-10193950.post-1130968217656485712005-11-02T22:45:00.000-05:002005-11-02T16:50:17.656-05:00Check for Numeric Values in a String using VBAI needed to check that the values entered into a textbox called StoreID in an <strong>Access Database</strong> where numerical and composed of 3 digits. Here is one solution: on the AfterUpdate event of the textbox, put this code:<br /><br /><span style="color:#000099;">Private Sub StoreID_AfterUpdate()<br /> Dim intLen, intChar, i As Integer<br /> Dim strTextBox As String<br /> <br /> intLen = Len(StoreID) <span style="color:#009900;">'Len is a function that counts the amount of characters in a string</span><br /> If intLen <> 3 Then <span style="color:#009900;">' if the length is not 3, give message box</span><br /> MsgBox "The field 'Store ID' requires 3 digits from 001 to 900"<br /> Else <span style="color:#009900;">' if length is 3 then assign variables and start loop for checking numerical values</span><br /> intLen = Len(Me!StoreID)<br /><span style="color:#009900;"> 'Me! always refers to the active form</span><br /> strTextBox = Me!StoreID<br /> For i = 1 To 3 <span style="color:#009900;">'For 1 to 3</span><br /> intChar = Mid(strTextBox, i, 1) <span color="#009900">'<strong>Mid function</strong> takes middle values (expression,start value, length of selection)</span><br /> If IsNumeric(intChar) = False Then<br /> MsgBox " ' " & intChar &amp; " ' is not a number, please enter numeric values only from 001 to 900"<br /> Exit Sub<br /> Else<br /> End If<br /> Next<br /> End If<br />End Sub</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-113096821765648571?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com1tag:blogger.com,1999:blog-10193950.post-1130967933382845432005-11-02T21:34:00.000-05:002005-11-02T16:45:33.403-05:00Call an Access Database Procedure from another Access DatabaseHere is an <strong>Access VBA example of how to call an external Access Database VBA procedure from within a different access database</strong>.<br /><br /><span style="color:#000099;">Sub ForeignProcedure()<br /><br />Dim appAccess As Access.Application<br /><br /><span style="color:#009900;">' Create instance of <strong>Access Application</strong> object.</span><br />Set appAccess = CreateObject("Access.Application")<br /><br /><span style="color:#009900;">' Open MyDatabaseName.mdb database in <strong>Microsoft Access</strong> window.</span><br />appAccess.OpenCurrentDatabase “C:\MyDatabaseName", False<br /><span style="color:#009900;">'False so as not to open in exclusive mode<br />' Run Sub procedure.</span><br />appAccess.Run "ForeignProcedureName"<br />appAccess.Quit<br />Set appAccess = Nothing<br /><br />MsgBox "Done!"<br /><br />End Sub</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-113096793338284543?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com2tag:blogger.com,1999:blog-10193950.post-1130954382206055562005-11-02T20:53:00.000-05:002007-05-06T13:48:43.401-04:00Call an Access Database VBA Procedure from ExcelIn order to <strong>call an Access vba procedure from an Excel spreadsheet</strong>, the first thing you need in Excel is a Reference for the <strong>Microsoft Access Object Library</strong>, as seen in the picture below ...<br /><br /><img alt="Access Object Library" src="http://www.hawkanalytics.com/AccessBlogPics/Access%20Object%20Library.jpg" /><br /><br /><br />To see this screen, from within Excel, go to VBA (press ALT + F11), then select Tools from the top menu and then References<br /><br /><span style="color:#000099;">Public Sub ProcedureInAccess()<br /><br /><span style="color:#009900;">‘this VBA code is run in Excel</span><br />Dim acApp As Object<br />Dim db As Object<br />Set acApp = CreateObject("Access.Application")<br />acApp.OpenCurrentDatabase ("C:\MyDatabaseName.mdb")<br />Set db = CurrentDb<br />acApp.Run "NameOfProcedureInAccess"<br />acApp.Quit<br />Set acApp = Nothing<br /><br />End Sub</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-113095438220605556?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0tag:blogger.com,1999:blog-10193950.post-1130953353222637532005-11-02T20:34:00.000-05:002005-11-02T12:42:33.236-05:00Delete Rows from Access Database Table Using VBA and SQLUsing the DoCmd property you can <strong>delete all rows in an Access Database table using Access VBA and SQL<br /></strong><br /><span style="color:#000099;">Sub DeleteRows()<br /><br /><span style="color:#009900;">'Deletes all rows from tblName in an <strong>Access Database</strong></span><br /><br />DoCmd.RunSQL "DELETE tblName.* FROM tblName;"<br /><br />End Sub</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-113095335322263753?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com1tag:blogger.com,1999:blog-10193950.post-1129146304336808442005-10-09T19:41:00.000-04:002005-10-12T15:46:26.470-04:00Access Database Programmer ForumI recently came across a great resource for <strong>Access Database programming</strong>. It is a forum where you can post questions and receive knowledgeable answers from the forum's members. <a href="http://www.utteraccess.com/">http://www.utteraccess.com/</a><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112914630433680844?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0tag:blogger.com,1999:blog-10193950.post-1126644174521185972005-09-13T19:34:00.000-04:002005-09-13T16:42:54.543-04:00Transaction Log for Microsoft Access DatabaseHere is one of the most useful pieces of code I have every come accross the Internet for <strong>programming an access database</strong>. This code will track all changes made by a user, meaning it will keep track of what data was changed, who changed it and when it was changed. This functionality is usually called a transaction log or a change log. I have copied the code directly from this page:<a href="http://www.tek-tips.com/faqs.cfm?fid=291">http://www.tek-tips.com/faqs.cfm?fid=291</a> . The code was written by Michael Red a regular contributor at Tek-tips, which is a wonderful website that I have used many times for finding answers to many <strong>programming</strong> questions.<br /><br />I have learned a few things not explained in his original code:<br /><br />1) Put this code in a Module.<br />2) Call the basLogTrans function from the BeforeUpdate event of any form that you want to track changes.<br />3) Pass 3 values to the basLogTrans function: FormName, Name of Field with Unique ID, and the Unique ID.<br />In my implementation, I use Me as the form name, "CustomerID" as the Unique ID field, and CustomerID to pass the actual customer id value. So here is how I would call the basLogTrans function:<br /><br />Private Sub Form_BeforeUpdate(Cancel As Integer)<br /><br />Call basLogTrans(Me, "CustomerId", CustomerId)<br /><br />End Sub<br /><br />4) Mike says the code will work only on a form formated as single form, but I see it also works on a datasheet form.<br />5) He alos says that Access Security needs to be enabled, but I'm not sure it's true. When I used his original code for CurrentUser(), I got back "Admin" as the user that was making changes. But what I really want to know is the NT User ID of whoever is making the changes, therefore, I use Environ("Username"). If you have problems with the Environ function, please see here how to <a href="http://www.accessprogrammer.blogspot.com/2005/02/get-user-name-in-access.html">enable the Environ Function</a> properly.<br />6) The rest of the instructions are pretty clear, but just to be safe, remember to add a reference for DAO, bind the form to a table, but who's to say this won't work on a Select Query? I don't know, why don't you tell me later? Make 2 tables, one called tblHist and the other tblHistMemo. First create tblHist, with the fields indicated in code comments in the function called basAddHist, then make a copy of it, name it tblHistMemo and change the OldVal and NewVal fields to datatype Memo.<br /><br />That's it, it should work easily.<br /><br /><br /><br />OK, HERE IS THE ORIGINAL VBA CODE:<br />All this needs to be placed in a Module, and called from the BeforeUpdate event of a form.<br /><br /><br /><span style="color:#000099;">Public Function basLogTrans(Frm As Form, MyKeyName As Variant, MyKey As Variant) As Boolean<br /><br /><span style="color:#009900;">'Michael Red 8/1/2002 Transaction log for Ms. Access BOUND forms.<br /><br />'The following three functions and table definitions are<br />'(should be) sufficient to establish a 'poor mans' transaction<br />'log for Ms. Access bound forms.<br /><br />'As noted in basAddHist, you will need to create the tables<br />'for History (general fields) and Memo History (history of<br />'JUST memo fields).</span><br /><br /><span style="color:#009900;">'Also, note that these functions are designed to work in<br />'conjunction with Ms. Access Bound forms where the view is<br />'set to single form. Further, the history will only capture<br />'the user if the db App is "secured".<br /><br />'In the following, "MyKey" refers to the Record ID (Unique value)<br />'for the single record which the form is 'tied' to.</span><br /><br />Dim MyDb As DAO.Database<br />Dim MyCtrl As Control<br />Dim MyMsg As String<br />Dim Hist As String<br /><br /><br /><span style="color:#009900;">'In the Statement block below, basFlgValidRec is a routine which<br />'includes validation of all fields necessary to 'define' a record<br />'is valid. Commented out here, as I do not know what validation<br />'rules need to be checked/enforced.<br /><br />' If (Not basFlgValidRec) Then<br />' basLogTrans = False 'Let User know of FAILURE!!<br />' Exit Sub<br />' End If</span><br /><br /><br />For Each MyCtrl In Frm.Controls<br />If (basActiveCtrl(MyCtrl)) Then 'Consider replacing w/ test for "ControlSource"<br />If ((MyCtrl.Value <> MyCtrl.OldValue) _<br />Or (IsNull(MyCtrl) And Not IsNull(MyCtrl.OldValue)) _<br />Or (Not IsNull(MyCtrl) And IsNull(MyCtrl.OldValue))) Then<br />If (MyCtrl.ControlType = dbMemo) Then<br />Hist = "tblHistMemo"<br />Else<br />Hist = "tblHist"<br />End If<br />Call basAddHist(Hist, Frm.Name, MyKey.Name, MyCtrl)<br />End If<br />End If<br />Next MyCtrl<br /><br />basLogTrans = True 'Let User know sucess<br /><br />End Function<br />Public Function basActiveCtrl(Ctl As Control) As Boolean<br />Select Case Ctl.ControlType<br /><br />Case Is = acTextBox<br />basActiveCtrl = True<br /><br />Case Is = acLabel<br />Case Is = acRectangle<br />Case Is = acLine<br />Case Is = acImage<br />Case Is = acCommandButton<br />Case Is = acOptionButton<br />Case Is = acCheckBox<br />basActiveCtrl = True<br />Case Is = acOptionGroup<br />Case Is = acBoundObjectFrame<br />Case Is = acListBox<br />basActiveCtrl = True<br />Case Is = acComboBox<br />basActiveCtrl = True<br />Case Is = acSubform<br />Case Is = acObjectFrame<br />Case Is = acPageBreak<br />Case Is = acPage<br />Case Is = acCustomControl<br />Case Is = acToggleButton<br />Case Is = acTabCtl<br /><br />End Select<br />End Function<br />Public Function basAddHist(Hist As String, Frm As String, MyKeyName As String, MyCtrl As Control)<br /><br /><span style="color:#009900;">'tblHist<br />'FrmName Text 80 Name of the form where change Occured<br />'FldName Text 80 Field Name of the changed value<br />'dtChg Date/Time 8 Date/Time of Change (MACHINE value!!)<br />'OldVal Text 255 Field Value BEFORE change<br />'NewVal Text 255 Field Value after change<br />'UserId Text 50 User who Made Change<br />'MyKey Variant ?? KeyField as Indentified by Caller<br />'MyKeyName Text 80 'Key Field Contents<br /><br />'tblHistMemo is the same structure except the "type" for the fields<br />'OldContents and NewContents are Memo (and therfore the length is "??")</span><br /><br />Dim dbs As DAO.Database<br />Dim tblHistTable As DAO.Recordset<br /><br />Set dbs = CurrentDb<br />Set tblHistTable = dbs.OpenRecordset(Hist, dbOpenDynaset)<br /><br />With tblHistTable<br />.AddNew<br />!MyKey = Forms(Frm).Controls(MyKeyName)<br />!MyKeyName = MyKeyName<br />!frmName = Frm<br />!FldName = MyCtrl.ControlSource<br />!dtChg = Now()<br />' !UserId = Environ("Username") <span style="color:#009900;">'Orlando's Way</span><br />!UserId =CurrentUser() <span style="color:#009900;">'Mike's way, example of current user would be Admin</span><br />!OldVal = MyCtrl.OldValue<br />!NewVal = MyCtrl<br />.Update<br />End With<br /><br />End Function<br /><br /></span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112664417452118597?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com3tag:blogger.com,1999:blog-10193950.post-1125611162601182012005-09-01T20:43:00.000-04:002005-09-01T17:48:27.643-04:00First and Last Access Function not supported in SQL ServerIn a <strong>Microsoft Access database</strong>, in order to return the first or last record in the result set returned by a query, you can use the First and Last Functions. The syntax is simple: <strong>First(expr)</strong> or <strong>Last(expr)</strong>. If you try to use these functions in <strong>SQL Server</strong>, however, they will not work. You will get a message similar to this:<br /><br /><span style="color:#ff0000;">Server: Msg 195, Level 15, State 10, Line 2<br />'First' is not a recognized function name.</span><br /><br />In order to repeat the desired outcome in SQL Server, simply use the <strong>MIN</strong> and <strong>MAX</strong> function.<br /><br /><strong>ACCESS SQL VERSION:</strong><br /><span style="color:#3333ff;">SELECT<br />ContractorID, FIRST(Contractor)<br />FROM<br />tblContractor<br />WHERE<br />ContractorID = "ABC6783"<br />GROUP BY<br />ContractorID</span><br /><br /><strong>(T-SQL) SQL SERVER VERSION:<br /></strong><span style="color:#3333ff;">SELECT<br />ContractorID, MIN(Contractor)<br />FROM<br />tblContractor<br />WHERE<br />ContractorID = 'ABC6783'<br />GROUP BY<br />ContractorID</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112561116260118201?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0tag:blogger.com,1999:blog-10193950.post-1125610986779142772005-08-29T19:40:00.000-04:002005-09-01T17:50:05.566-04:00Loop in VBA Easy ExampleThere are different versions of <strong>looping in VBA</strong>. Here is an example of the Do While version.<br /><br /><span style="color:#3333ff;">Sub letsLoop()<br />Dim intCounter As Integer<br /><br />intCounter = 1 <span style="color:#009900;">'Set the counter to 1</span><br /><br />Do While intCounter <3<span style="color:#009900;">'Loop while value of intCounter less then 3<br /></span>MsgBox intCounter <span style="color:#009900;">'show message box with value of intCounter<br /></span>intCounter = intCounter + 1 <span style="color:#009900;">' increase value of intCounter by 1</span><br />Loop <span style="color:#009900;">'loop again<br /></span><br />End Sub </span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112561098677914277?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com1tag:blogger.com,1999:blog-10193950.post-1124992636535317462005-08-15T19:55:00.000-04:002005-08-28T11:48:46.243-04:00Capture Signature and Date Stamp in Excel WorksheetWith all the <strong>Sarbanes-Oxley (SOX)</strong> requirements going around lately, there has been a big push to making staff attest that they have reviewed the various <strong>Excel reports</strong> they are sent. One way to do that is to have them sign and date the report and save the report somewhere on the network. Later I will run an <strong>Excel macro</strong> that goes and looks for everyone’s signature and produces a report of who has signed and when.<br /><br />Anywho, for the signature portion, I've added a sheet in the Excel reports, the sheet is called Signature. This sheet has a check box. Clicking this check box called chkSignature, populates cells C2 and C3 with the Windows User ID and the Date.<br /><br />For the user's Windows User ID, we use the method Environ("username"). And for the Date we use well, the Date function.<br /><br />Here is the code behind the click event of check box chkSignature:<br /><br /><span style="color:#3333ff;">Private Sub chkSignature_Click()<br /><br />If chkSignature.Value = True Then<br />Sheets("Signature").Range("C2").Value = Environ("username")<br />Sheets("Signature").Range("C3").Value = Date<br />Else<br />Sheets("Signature").Range("C2").Value = ""<br />Sheets("Signature").Range("C3").Value = ""<br />End If<br /><br />End Sub</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112499263653531746?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com3tag:blogger.com,1999:blog-10193950.post-1123719689043708162005-08-10T20:17:00.000-04:002005-08-10T20:21:29.050-04:00Automatically Maximize Access FormTo maximize an Access form automatically, use the "<strong>Maximize</strong>" method of the <strong>DoCmd</strong> Object. <br /><br />Example, let's say I have a form called MainForm, on the "<strong>On Load</strong>" Event property of the form, I would use this sub procedure:<br /><br /><span style="color:#000099;">Sub MainForm_Load()</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;"> DoCmd.Maximize</span><br /><span style="color:#000099;"></span><br /><span style="color:#000099;">End Sub</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112371968904370816?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com2tag:blogger.com,1999:blog-10193950.post-1123532552536980682005-08-08T19:20:00.000-04:002005-08-08T16:22:32.543-04:00Find the Number of Series in Excel ChartWhile adding a trend line to a group of charts, I needed to know how many data series where in the Graph's Series Collection. I will add an equal amount of trend lines.<br /><br /><br /><span style="color:#000099;">Sub countseries()<br /><br />MsgBox ActiveChart.SeriesCollection.Count<br /><br />End Sub</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112353255253698068?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0tag:blogger.com,1999:blog-10193950.post-1122313676015926962005-07-25T20:45:00.000-04:002007-05-06T14:37:07.581-04:00Using a Combo Box in ExcelFrom the top menu, navigate thru: <strong>View / Toolbars / Control Toolbox<br /></strong><br /><img src="http://www.hawkanalytics.net/AccessBlogPics/Excel_Combobox_1.jpg" /><br /><br />When the Control Toolbox appears, Select the Combo Box control. With your mouse, make a rectangular shape on the excel sheet.<br /><br /><img src="http://www.hawkanalytics.net/AccessBlogPics/Excel_Combobox_2.jpg" /><br /><br />Define the choices that will be available in the Combo Box. In this example, the names of fruits are written on <strong>Column F from row 1 thru 4.</strong><br /><br /><img src="http://www.hawkanalytics.net/AccessBlogPics/Excel_Combobox_4.jpg" /><br /><br /><br />Tell the combobox where to get it's data from. Right click on the combobox to get it's properties.<br /><br /><img src="http://www.hawkanalytics.net/AccessBlogPics/Excel_Combobox_5.jpg" /><br /><br /><br />Then in the ListFillRange, enter the range of cells that hold the combox values. In this case <strong>F1:F4.<br /></strong><br /><img src="http://www.hawkanalytics.net/AccessBlogPics/Excel_Combobox_6.jpg" /><br /><br />That's all there is to it, close design view and your combo box should be working fine.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112231367601592696?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0tag:blogger.com,1999:blog-10193950.post-1121795672939065192005-07-19T19:46:00.000-04:002005-07-19T13:56:55.963-04:00Securing an Access DatabaseA reader from Kuwait wrote the other day...<br /><br /><br />Orlando,<br /><br />I was browsing for some help on the net and just saw<br />your name, I had a question, could you please help me.<br /><br />My question is, i have developed a database, and i<br />want to give it to my friend, the question is, i want<br />to secure the Queries, Form & Reports from being<br />edited or re-designed, i dont want these to be visible<br />whenever he uses it. he should be able to print &amp;<br />update database, but he should not be able to see what<br />queries i have set as well he should not be able to go<br />to the design view to modify my forms &amp; Reports.<br /><br />Could you please help.<br /><br />Regards,<br /><br />Oswald<br />Kuwait<br /><br /><br />Oswald...<br /><br />There are a couple of different ways that the above can be accomplished.<br /><br />1) User Level Security. But I don't recommend it because it is difficult to maintain.<br /><br />2) A combination of setting the Start Up options, tweaking the properties of the Forms, and making an mde file.<br /><br />You will have to play around with option number 2 to get the desired results. But here are a few pointers.<br /><br />From the menu bar, go to Tools / Start Up and un-check "Display Database Window". Next time the database starts, the database window will not be available. If you want to see it, then you would go to Window / Unhide and select the database window. If you choose to not see the database window, you would have to have some form that would serve as the main menu for your database.<br /><br /><img src="http://www.accessdbpro.net/AccessBlogPics/Access_Hidden_2.jpg"><br /><br /><br />Another thing that you could do is hide your database objects, but this is the least effective way, because in the Database Options there is the option to View Hidden Objects. But anyway, here is how you could hide them: Right Click on the object to arrive at the Properties window and check Hidden.<br /><br /><img src="http://www.accessdbpro.net/AccessBlogPics/Access_Hidden_1.jpg"><br /><br /><br />Another alternative is to allow Design Changes on Forms Only in Design View. And to not allow viewing the form any other way then you intended. Forms can be viewed as FORMS, DATASHEETS, PIVOT TABLES, PIVOT CHART. So make sure that in the Forms Format Properties you unselect the view types you do not want the user to use.<br /><br /><img src="http://www.accessdbpro.net/AccessBlogPics/Access_Hidden_3.jpg"><br /><br /><br /><img src="http://www.accessdbpro.net/AccessBlogPics/Access_Hidden_4.jpg"><br /><br /><br />Finally, after you have implemented a combination of the tips above, make the database an MDE file. Go to Tools / Database Utilities / Make MDE file.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112179567293906519?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0tag:blogger.com,1999:blog-10193950.post-1121289874820678032005-07-10T19:22:00.000-04:002005-07-13T17:24:34.820-04:00How to Filter Access Subforms based on a control from a Main FormIf you are reading this then you have probably noticed that filtering a Subform based on a control from the Main form is a pain in the neck.<br /><br />Here's one way to filter the subform by changing the underlying form.<br /><br /><span style="color:#000099;">Sub frameTaskStatus_Click()<br /> Dim intStat As Integer<br /> intStat = frameTaskStatus.Value<br /><span style="color:#009900;"> ' 1 = all<br /> ' -1= closed<br /> ' 0 = open<br /> ' 2 = new open frmCreateNewRequest</span><br /> <br /> Select Case intStat<br /> Case -1<br /> Me!sbfrmMainRequests.SourceObject = "subfrmRequestsFiltered"<br /> Case 0<br /> Me!sbfrmMainRequests.SourceObject = "subfrmRequestsFiltered"<br /> Case 1<br /> Me!sbfrmMainRequests.SourceObject = "subfrmRequestsAll"<br /> Case 2<br /> Me!sbfrmMainRequests.SourceObject = "subfrmRequestsNew"<br /> End Select<br /><br />End Sub</span><br /><br /><br /><strong>Cases -1 and 0</strong> (closed and open, respectively) make subfrmRequestsFiltered the underlying subform. This subform's datasource is a query that applies a filter also based on the value selected in frameTaskStatus.<br /><br /><strong>Case 1</strong> makes subfrmRequestAll the underlying subform in the Main form. subfrmRequestAll is also based on a query but it is not filtered.<br /><br /><strong>Case 2</strong> makes subfrmRequestsNew the underlying subform in the Main form. subfrmRequestsNew is a data entry only form.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112128987482067803?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com1tag:blogger.com,1999:blog-10193950.post-1120684676633644332005-07-06T19:16:00.000-04:002005-07-06T17:49:50.020-04:00How to Determine the Version or Edition of SQL ServerOpen Query Analyzer, connect to any database on your server and type one of the following:<br /><br /><span style="color:#000099;">SELECT SERVERPROPERTY('ProductLevel')<br />SELECT @@VERSION<br />SELECT SERVERPROPERTY('ProductVersion')</span><br /><br />If you are not sure whether you are running an edition of the SQL Server 2000 database engine or MSDE 2000, execute the following query:<br /><span style="color:#000099;">SELECT SERVERPROPERTY('Edition')</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112068467663364433?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com3tag:blogger.com,1999:blog-10193950.post-1121290119606746932005-06-28T18:28:00.000-04:002005-07-13T17:28:39.606-04:00How to run an Access Macro from Access VBA<strong><span style="color:#000099;">DoCmd.RunMacro "macroName"</span></strong><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112129011960674693?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0tag:blogger.com,1999:blog-10193950.post-1119392051366181282005-06-21T19:13:00.000-04:002005-06-21T19:52:09.730-04:00A word of Advice for my International Programming BrothersCollation is the way in which data will be sorted in SQL Server.<br /><br />When building SQL Server databases for American clients, be sure to define the database collation as <strong>SQL_Latin1_General_CP1_CI_AS</strong>, which means Latin characters, case insensitive, accent sensitive. This is the default setting for Windows Desktops in America. If you are developing in another country, your installation of Windows will probably have a different default language setting. If when you created the SQL Server database, you chose “default” for the collation settings, the database will have been created with a different collation setting, a non-American collation setting.<br /><br />If you are developing a multi-language capable database, an international database, define your table fields as nvarchar whenever possible. Nvarchar ia a UNICODE character, which means it uses two bytes per character, as opposed to ASCII characters which are one byte in length and thus limits the number of possible ASCII characters<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-111939205136618128?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0tag:blogger.com,1999:blog-10193950.post-1121290059792349562005-06-16T18:26:00.000-04:002005-07-13T17:29:15.263-04:00Access: How to Refresh Form Data or Data on a ControlSimple:<br /><br />use the <strong><span style="color:#000099;">.Requery</span></strong> Method<br /><br /><span style="color:#000099;"><strong>Forms!FormName.ControlName.Requery</strong><br /></span><br />or if you want to refresh all controls in the form, then<br /><br /><strong><span style="color:#000099;">Forms!FormName.Requery</span></strong><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112129005979234956?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com2tag:blogger.com,1999:blog-10193950.post-1121289948528867082005-06-13T19:24:00.000-04:002005-07-13T17:25:48.530-04:00User Friendly Prompt for Requesting Required Information in Access FormI have a form which has a SAVE RECORD button, which runs a Save Record Macro. The underlying form's table has a Required field. When the Required field is not populated and the SAVE RECORD button is clicked, a popup message prompts the user for the Required field. The problem is that the popup message prompts for the field name as defined in the table and in some cases this might be confusing to the user.<br /><br />In order to make a user friendly popup message, you could add a condition to the Save Record Macro which checks whether the Required field control is populated or not with:<strong> <span style="color:#000099;">is null(Required Field)</span>.</strong> If it is true, exit the macro, else continue.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112128994852886708?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com1tag:blogger.com,1999:blog-10193950.post-1121289762172926382005-06-05T22:21:00.000-04:002005-07-13T17:22:42.176-04:00How to Run an Access Macro from Excel VBAIn excel you would need the following Sub procedure:<br /><br /><span style="color:#000099;">Sub RunMacroInAccess()<br /><br />Dim Acc as Object<br />Set Acc = CreateObject("Access.Application")<br />Acc.OpenCurrentDatabase("C:\My Documents\AccessDatabase.mdb")<br />Acc.DoCmd.RunMacro "AccessMacroName"<br /><br />End Sub</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-112128976217292638?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com1tag:blogger.com,1999:blog-10193950.post-1117744683658894022005-06-02T19:34:00.000-04:002005-06-02T16:38:03.663-04:00Run an Excel Macro from an Access DatabaseIf you need to run an Excel Macro from within an Access database, place this vba code on the click event of a command button called cmdExcel_Macro. There is no need to add an Excel Reference Library in Access.<br /><br /><span style="color:#000099;">Private Sub cmdExcel_Macro_Click()<br /><br />Dim excelApp As Object <span style="color:#009900;">' Declare Excel Application as Object<br /></span>Dim workbook As Object <span style="color:#009900;">' Declare Excel Workbook as Object<br /></span>Set excelApp = CreateObject("Excel.Application")<br />Set workbook = excelApp.Workbooks.Open("C:\Temp\FileName.xls") <span style="color:#009900;">' Full path of Excel book</span> that has Macro<br />excelApp.Run "ExcelMacroName" <span style="color:#009900;">' Name of the Excel Macro to run<br /></span>workbook.Close False<br />excelApp.Quit<br />Set excelApp = Nothing<br /><br />End Sub</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-111774468365889402?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0tag:blogger.com,1999:blog-10193950.post-1116874534470411212005-05-23T19:54:00.000-04:002005-05-24T18:35:33.026-04:00Loop Through Charts in Excel WorksheetHere’s a quick piece of code that will generate message boxes for every chart in the active worksheet with the name and top position of the chart<br /><br /><span style="color:#000099;">Sub LoopThruCharts()<br /><br />For i = 1 To ActiveSheet.ChartObjects.Count<br />ActiveSheet.ChartObjects(i).Select<br />MsgBox ActiveSheet.ChartObjects(i).Name<br />MsgBox ActiveChart.Parent.Top<br />Next i<br /><br />End Sub</span><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-111687453447041121?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com1tag:blogger.com,1999:blog-10193950.post-1116857009037712842005-05-22T11:01:00.000-04:002005-05-23T10:27:21.523-04:00Detect and Remove SpywareRun the free version of this program called Ad-aware on your computer and I bet you will be surprised by the amount of spyware or tracking cookies that have been installed on your computer without your knowledge. I ran this product after I started noticing that whenever I Googled something, pop-ups would appear on my screen advertising products or services related to my search terms. I thought first of all, Google does not have pop-ups, and second, how the heck is it that these pop-up screens seem to know exactly what I'm searching for.<br /><br />Anyway, I have used this product on different types of operating systems and it has never created any problems.<br /><br />You can find the program at this site: <a href="http://www.lavasoftusa.com">www.lavasoftusa.com</a><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/10193950-111685700903771284?l=accessprogrammer.blogspot.com'/></div>Access Programmer -- Orlandohttp://www.blogger.com/profile/12369020510995748083orlandog7@gmail.com0