hi! folks merry christmas and happy new year to all of you
ahhm! i have problem in using the .bas module i mean declaring global varibles from it, I try several times to use the variable that i declared in .bas module but did not work so can you help me.. and give some example program from it

hoping for your kindness


God bless all

Dani AI

Generated

A few practical clarifications about .bas modules and "global" variables in classic Visual Basic (VB6). demonstrated the basic idea; asked for details — the usual causes when a module-level variable seems to "not work" are scope mistakes, shadowing by local variables, initialization-order issues, or a project reset wiping module state. It is not primarily a memory-size problem.

Common checklist to diagnose a missing value:

  • Declaration must be at module scope (top of a standard .bas), not inside a Sub/Function.
  • Use Option Explicit and compile to catch typos that create new, unintended variables.
  • Check for a local variable or a control with the same name that shadows the module name.
  • Module-level state is cleared when the project is stopped, when End is used, or after an unhandled error — explicit initialization is safer.
  • If sharing state across separate EXE/DLL projects, module visibility and export rules differ.

A safer pattern than lots of bare globals is a tiny application-state class exposed through a single well-initialized instance. Example class (Class module named CAppState) and standard module init:

' Class module: CAppState
Option Explicit
Private mUserName As String

Public Property Get UserName() As String
    UserName = mUserName
End Property

Public Property Let UserName(ByVal v As String)
    mUserName = v
End Property
' Standard module
Option Explicit
Public AppState As CAppState

Public Sub InitApp()
    Set AppState = New CAppState
End Sub

Call InitApp at program startup (Sub Main or main form load). This keeps state encapsulated, makes debugging easier, and limits accidental name collisions.

For a one-week lending-system project: pick a simple datastore (Access .mdb via ADO or CSV), implement core tables (Books, Members, Loans), focus first on borrow/return logic (check availability, create loan, decrement/increment counts), then add simple search and validation, and finish with basic reports. Prioritize a working checkout/return flow and clear transaction/error handling — additional features (fines, detailed reports) can be deferred.

Recommended Answers

All 4 Replies

Hi, Happy Christmas and Happy New Year.

Lets come to the discussion,
Use Public keyword declare a public variable in a module so that all other modules and forms can access it.

Ex:-

'In a module
   Public sUserName As String

'In a Form (Form1 )
' - Draw a Button (Command Button - cmdSetUser)
' >> Add Form (Form2)
' - Draw a Button (Command Button - cmdShowUser)

'In Form1
Option Explicit

Private Sub cmdSetUser_Click()
   sUserName = "ABCD"
   Form2.Show
End Sub
'In Form2
Option Explicit

Private Sub cmdShowUser_Click()
   MsgBox sUserName
End Sub

But avoid often using global variables.

hey! thanks brother ur such a generous person now i know how to use it ahhm! bro why do you say that avoid often using global variable is it cause a lot of memory but thanks anyway,
but bro i have a big problem about my project its a lending system
our teacher give this kind of project and he told us to self study only
and you know i have only 1 week to finish it and i dont know what to do bro can you help me pls

When u use global variable, u may face some problems .
See this site for detail. It is common for all the languages. So dont use it often.

Dont worry about your project.

Just try out your project, if u have any doubt u post it here.
Definitely not only myself, any one can help you .. but try from your side is very important .. Do well.

nice. what kind of problem u faced u not mentioned. so tell it clearly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.