Microsoft KB Archive/143044

= INFO: How Visual Basic 4.0 Handles Files Marked Read-Only =

Article ID: 143044

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
 * Microsoft Visual SourceSafe 4.0 Standard Edition
 * Microsoft Visual SourceSafe 4.0 Standard Edition

-



This article was previously published under Q143044



SUMMARY
Visual Basic 4.0 may handle files with the read-only attribute set differently than Visual Basic 3.0. This may result in a "Can't Edit Module" error when trying to change a code module. The Visual Basic 4.0 development environment will handle read-only files in one of two ways. The way Visual Basic 4.0 behaves is dependent on the current setting of the ReadOnlyMode property of the development environment. If the ReadOnlyMode is set to 0 (lenient) you will be able to edit files marked read-only just like Visual Basic 3.0. Of course you will still not be able to save your changes. If the ReadOnlyMode is set to 1 (strict) Visual Basic will not allow you to edit files marked read-only.



MORE INFORMATION
The default ReadOnlyMode for Visual Basic 4.0 is lenient. However, the default can be changed to strict by adding ReadOnlyMode=1 to the Visual Basic section of the VB.INI.

ReadOnlyMode can also be changed dynamically by Visual Basic 4.0 add-ins through the ReadOnlyMode property of the Application object of Visual Basic 4.0 add-in object model. The Microsoft SourceSafe add-in for Visual Basic 4.0 sets Visual Basic 4.0 to the strict mode. The following example shows you how to create a add-in that will allow you to toggle the ReadOnlyMode from a menu item.

Example
 Start a new project. Remove the default Form1. Add a reference to "Microsoft Visual Basic 4.0 Development Environment" using the Tools/References dialog. Add a new standard module (Module1.BAS by default).  Put the following code in the standard module: ''' MODULE1.BAS '''     #If Win16 Then Declare Function WritePrivateProfileString Lib "KERNEL" ( _           ByVal AppName As String, ByVal KeyName As String, _            ByVal keydefault As String, ByVal FileName As String) As _ Integer Declare Function GetPrivateProfileString Lib "KERNEL" ( _           ByVal AppName As String, ByVal KeyName As String, _            ByVal keydefault As String, ByVal ReturnString As String, _            ByVal NumBytes As Integer, ByVal FileName As String) As Integer #ElseIf Win32 Then Declare Function WritePrivateProfileString Lib "KERNEL32" _ Alias "WritePrivateProfileStringA" (ByVal AppName As String, _           ByVal KeyName As String, ByVal keydefault As String, _            ByVal FileName As String) As Long Declare Function GetPrivateProfileString Lib "KERNEL32" _ Alias "GetPrivateProfileStringA" (ByVal AppName As String, _           ByVal KeyName As String, ByVal keydefault As String, _            ByVal ReturnString As String, ByVal NumBytes As Long, _            ByVal FileName As String) As Long #End If

Sub Main #If Win16 Then Const Section = "Add-Ins16" #ElseIf Win32 Then Const Section = "Add-Ins32" #End If        Const BufSize = 255

Dim Ret As Variant Dim RetStr As String

' Check to see if the entry is already in the VB.INI file. ' Add if not.

RetStr = Space(BufSize) Ret = GetPrivateProfileString(Section, "ReadOnlyMode.Switch", _           "NotFound", RetStr, BufSize, "VB.INI") RetStr = Left(RetStr, Ret) If RetStr = "NotFound" Then WritePrivateProfileString Section, "ReadOnlyMode.Switch", _ "0", "VB.INI" End If     End Sub

''                        Add a new class module to the project (Class1.CLS by default.)  Set the following properties of the class module to the specified values: <pre class="fixed_text">     Property        Value ---

Instancing     1 - Creatable MultiUse Name           Switch Public         True </li>  Put the following code in the class module: ''' CLASS1.CLS      Option Explicit

Private ThisInstance As VBIDE.Application Private AddInMenuLine As VBIDE.MenuLine Private AddInID As Long

Public Sub ConnectAddIn(VBInstance As Object) Set ThisInstance = VBInstance Set AddInMenuLine = ThisInstance.AddInMenu.MenuItems.Add( _           "Strict &ReadOnlyMode") AddInMenuLine.Checked = ThisInstance.ReadOnlyMode AddInID = AddInMenuLine.ConnectEvents(Me) End Sub

Public Sub DisconnectAddIn(Mode As Integer) AddInMenuLine.DisconnectEvents AddInID ThisInstance.AddInMenu.MenuItems.Remove AddInMenuLine End Sub

Public Sub AfterClick ThisInstance.ReadOnlyMode = ThisInstance.ReadOnlyMode Xor 1 AddInMenuLine.Checked = ThisInstance.ReadOnlyMode End Sub

''                       </li>  Set the following Project Options: <pre class="fixed_text">     Option            Value

Startup Form     Sub Main Project Name     ReadOnlyMode StartMode        OLE Server Error Trapping   Break in Class Module </li> Save the project.</li> Run the project.</li> Start up a second instance of Visual Basic (Form1 is created by default.)</li> Use the Add-In Manager to add the ReadOnlyMode.Switch add-in.</li> Open a code module with that has the read-only attribute set. You can tell if a file is marked read-only by the small red lock that appears in the project window next to the file.</li> From the Add-In menu you can switch the ReadOnlyMode. When checked the environment will be in strict mode and when unchecked it will be in lenient mode.</li></ol>

Additional query words: Addin Addins VBIDE ssafe

Keywords: kbinfo kbenv KB143044

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.