24. August 2007 12:54

MS Access: Let lines in Reports grow with it’s content

The problem is the following: You create a report with MS Access with a text field. To display the whole text, you have to set the “CanGrow” property to “Yes” (on the section and on the text-field). When running the report that has vertical lines as column seperator you have an ungly effect: The line keeps its size, as this has been fixed in the design view.

One might think that the easy solution is to adjust this length at runtime. First of all I need to declare what “runtime” means. As this problem occures in every “Detail”-Section we must dive in the events a section provides. There are three of them “OnRetreat”, “OnFormat” and “OnPrint” (apart all the mouse events, which are defenitly not suitable).

The Retreat-Event occurs “when Microsoft Access returns to a previous report section during report formatting” (Source: VBA Help). Not exactly what we’re looking for.

The Format-Event occurs “when Microsoft Access determines which data belongs in a report section, but before Access formats the section for previewing or printing”. It sounds good, but at this point we don’t have our data in the section and are therefore unable to determine the correct height.

Finally the Print-Event “after data in a report section is formatted for printing, but before the section is printed”. Here we are, we have our data which is not yet printed. The problem is that at this point all elements are already formatted, all we can do is add new elements and that’s why we can’t just adjust the height of each line by setting it via

 Me.myLine.Height = Me.Height '''this does not work in the OnPrint event, myLine has already been formatted

The simple solution is to draw a new line and use your already placed line as a template:

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
    Dim EndOfLine As Double
    EndOfLine = Me.Height       ''height of section (where the line stops)

    ''becasue it's not allowed in such a late state to change lines, we draw new one's based on the old
    ''myLine
    Me.Line (Me.myLine.Left, 0)-(Me.myLine.Left, EndOfLine)

End Sub;

Tags:  MS Access  VBA  Programming 
Comments
30. June 2007 01:49

MS Access and not updatable Recordsets

The whole thing is just plain stupid: Someone in the department I work in has created an MS Access Database to track our customers. Above this application are some forms to insert the data. Now we already have much of the data in various excel sheets etc. So my task was to write a function (or method or sub or whatever you call it in your world) to get some values from an excel sheet and insert it to a form, just like a user would do it. Like that the user has the possibility to check and change the data before it gets saved in the database. Now if you bind the form elements to parts of a query, Access gets your data as a so called snapshot via MS Jet (your communication channel to the DB). Snapshot data is not updateable, so if you try to assign your own values to form elements an error message pops up and that’s it.

My first solution was to ignore that fact and simply update my data directly to the database. But then all nice features like the “undo” option (to reset a record) or the possibility to check values before they get inserted are lost. The solution I came up is simple, but you need to check your status very exact to keep a usable form.

I have a form with my data, I have a save button, I have an exit button and I have an update button. There is a special button to start the gathering of data from the excel sheet. Now If you want to change the value of a textbox on the form you need to cut the textbox off the data (ControlSource = ""), then you can freely change the value.

Now you can’t simply update the current record, because the textbox is no longer in the recordset. This means that you have to update the value directly in the database when the save button is pressed. Then reattach the ControlSource and Requery and Repaint your form.

When you want to Undo your changed, just don’t save your values in the DB and reassign the ControlSource and Refresh the form, the old value will appear.


Tags:  MS Access  Programming 
Comments