Please help to understand Sub Routine code...

RompStar

Well-known Member
Joined
Mar 25, 2005
Messages
1,200
I am learning Access slowly, I know some things and others I don't. We have a Metric system that has some bugs that I need to figure out where they are at and what is causing them, as the person who used to do this, found a better possition somewhere else. So I started with the VBA code from the Form. There is a button on the Form, I press it and it runs this code:

I commented after the code, so it fits in the window here... I need help with understand what they are, the ones with ? I understand that this Sub Routine calls others in different places, so I have made a map of all the /Modules that exists, and I make a list of all the Subs and Functions that exists within module, so that I can follow when my VBA Form code calls them and where they are so that I can refer to them, did I do this right ?

Please help out just to understand this code, and then I'll go from there, and follow the links and examine other calls as I follow this code. I am reading an Access book, so hopefully it will help me out further, am already 1/2 way through it.

Private Sub cmdRecDist_Click()
' Start of Sub Routine: cmdRecDist_Click()

On Error GoTo Err_End
' On error goto line that starts with Err_End and continue from there, skip everything

CursorHourGlass
' Call Sub Routine from /Modules/General

ADOCurrentProjectConnect
' Calls Sub Routine from /Modules/General

PopulateTableRECDISVOL
' Calls Sub Routine from /Modules/RECDISTdata

CalRecNonConv1
' Calls Sub Routine from /Modules/RECDISTdata

CalDisNonConv2
' Calls Sub Routine from /Modules/RECDISTdata

CloseConnection
' Calls Sub Routine from /Modules/General

CursorNormal
' Call Sub Routine from /Modules/General

MsgBox ("Complete")
' Display Message Box with the word: Complete to inform user that Form procedure has ended.

Exit Sub
' Exit Subroutine

Err_End:
' Jump point for Error Handling starts here from GoTo statement above

ErrEndConnect
' Calls Sub Routine ErrEndConnet() below

End Sub
' Sub Routine Ends

Sub ErrEndConnect()
' Start of Sub Routine: ErrEndConnect()

If (cnn.State = adStateOpen) Then
cnn.Close
End If
' If Statement. variable cnn = Public cnn As New ADODB.Connection
' ADO - ActiveX Data Objects, connecting to a DataBase to gain access to data source, .Connection = object. If cnn connection.state = Open, then Close the connection and End If statement.

Set cnn = Nothing
' Set cnn variable to null

If (cnn2.State = adStateOpen) Then
cnn2.Close
End If
' Same as above but using cnn2 variable for ADO connection

Set cnn2 = Nothing
' Set cnn2 variable to null

Close #1
' ????? Close #1 ? what is #1 ?

CursorNormal
' Call Sub Routine from /Modules/General

MsgBox (Err.Description)
' Display Message Box with the Error Description

End Sub
' Sub Routine ends.

------------------------

Thank you for your help, if I made a mistake please show where and explain, thanks!!! :rolleyes: :LOL:
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

VicRauch

Well-known Member
Joined
Mar 23, 2006
Messages
2,032
RompStar
The Close #1 is to close a file that has been opened somewhere in one of the routines that were called in the first procedure, "Private Sub cmdRecDist_Click() " Within Basic, you (as the programmer) can open files. Each file you would open would have a number associated with it. Normally, when people use files like this, they number the files starting with #1. So, whatever file was opened, this routine is just closing everything that was opened, and file #1 is just one of the things that got opened.
This second routine, that is called if an error is found, is used to close all possible open objects. That way, any opject that was opened, that was not closed because the error caused the normal program flow to be inturupted, will be closed by this second Sub routine.
HTH
 

Watch MrExcel Video

Forum statistics

Threads
1,133,457
Messages
5,658,875
Members
418,475
Latest member
ExcelBeginner233

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top