Reorganize VB Code into modules and create public variables

jbwilks6

New Member
Joined
Oct 29, 2022
Messages
6
Office Version
  1. 2019
Platform
  1. Windows
I have inherited a vb project and it's pretty disorganized and I'm trying to clean it up. The main issue is I need to change the all the references to the connect string used to call the database. The project has several sheets, 6 user forms, and 1 module. Most of the code is spread around inside the form code. The module has several public functions that are called from various places inside the form code. There are about 20 references to the database spread around all the form code. Usually there is a command button_click event that builds the connect string and calls the db (in 20+ places). The connect string is hardcoded in each reference. I want to reorganize this so that I can maybe have one variable for the connect string (security is not an issue as all of this is on a local db).

I'm not sure where to begin. I tried creating some global variables but kept getting the 'invalid outside procedure error'. I know it needs to be defined inside a standard module but when i did this(Public strconnTest As String) , says "invalid attribute inside procedure".

So I'm looking for help to get the code organized/centralized and assign a variable once for connect string
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Public variables (constants) are declared at the beginning of the standard module, even before the first procedure. Because you declared inside the procedure you get a compilation error. In your case it seems that you should use a public constant.

Artik
 
Upvote 0

Forum statistics

Threads
1,223,876
Messages
6,175,129
Members
452,614
Latest member
MRSWIN2709

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
Back
Top