Inserting a Text File as Code

Posted on 1998-08-05
Last Modified: 2010-05-03
  I am a newbie to visual basic so any help would be greatly appreciated.  I am trying to create a configuation file for my visual basic project.  I need to assign variables different path names and I want to store this in a text configuation file that can be easily changed from computer to computer.  
   I can create a text file and read it using the Input funtion but I would rather have the text file execute as code when the main form loads.
   I have found a command called InsertFile () in the VBIDE extension but I can not figure out on how to use this function since it is an Add-in.  I have include the reference to this VBIDE in my Visual Basic Program but I do not know where to go from there.  I can't seem to set the formtemplate to the main form that loads at startup so that it will insert the text file as code.
   I do not know where to put the declarations and the functions -- should they be included in a separate module or can I put them in the main form code module??  Can you include some sample code for this problem??
   Thank you in advance

Can you send an email to me at
Question by:2helpornot2help
  • 2
  • 2

Expert Comment

ID: 1467902
You can raed file content as values for parameters to initialize your system, eith through text file or better in .ini file.

If you are working with VB5 or lower, you cannot read a text file and excute its content as code.


Author Comment

ID: 1467903
  I am using Visual Basic Version 5 and I think it is possible to do this without having to create and ini file, but how would you create an ini file for this project??
LVL 14

Expert Comment

ID: 1467904
As 2helpornot2help said, why don't use INI file. It is easier, you have API (see below). If you want, I can send you a module for handling INI file.

Another solution (more 32 bits) is using the registry. It is also very simple to use.

Declare Function OSGetPrivateProfileInt Lib "Kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Declare Function OSGetPrivateProfileSection Lib "Kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare Function OSGetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

LVL 14

Expert Comment

ID: 1467905
NB : For myself, I always use INI files to store informations concerning database path, path for working directories... and I read them at the startup.

I prefer INI files, because, I can modify them enventually through the network on the clients'computers.

Accepted Solution

DQ earned 100 total points
ID: 1467906
You can not input a text file to excute as code. Period.

The following to functions are for read and write to INI files.
If you need help with some basics of INI file, add comments.

To Write to a ini file:

Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal
lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long

Public Function fWriteINI(ByVal SectionHeader$, ByVal VarName, ByVal sNewString, ByVal filename$) As Long

'   Called procedures and functions:
'       WritePrivateProfileString
'   Input:
'       SectionHeader$   Name of section in INI file
'       VarName          Name of variable in section in INI file
'       sNewString       Value to be written as VarName's value in INI file
'       filename$        File name of INI file
'   Output:
'       Success          Status of the write operation

' To remove a line from an INI file, use the following
'r% = WritePrivateProfileString (Section Header, Variable Name, 0&, File Name)
'To Remove an entire section from an Ini file use this line...
'r = WritePrivateProfileString (Section Header, 0&, 0&, File Name)

Dim lWriteSuccess As Integer
    On Error GoTo fWriteIniErrHandler
    lWriteSuccess = WritePrivateProfileString(SectionHeader$, VarName, sNewString, filename$)
    fWriteINI = lWriteSuccess
    Exit Function

    fWriteINI = False
End Function

To read the INI file
' Declare
Declare Function GetPrivateProfilestring Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal _
lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal _
lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Public Function fGetFromIni(SectionHeader$, VarName$, filename$) As String

'   Called procedures and functions:
'       GetPrivateProfileString
'   Input:
'       SectionHeader     Name of section in ini file
'       VarName           Name of variable in ini file
'       FileName          Filename of the ini file
'   Output:
'       The value of the VarName variable contained
'       in the SectionHeader section in the FileName
'       ini file.
'   Note: This function requires to declare an API function GetPrivateProfilestring

   Dim lReqStr As String

   On Error GoTo fGetFromIniErrHandler

   lReqStr = String(255, Chr(0))
  'Get Requested Information
   fGetFromIni = Left(lReqStr, GetPrivateProfilestring(SectionHeader$, ByVal VarName$, "", lReqStr, Len(lReqStr), filename$))
   Exit Function

   fGetFromIni = "ERROR"

End Function


Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Paint/Redraw window while dragging 16 77
Using an encrypted  value to decrypt and display contents in vb6 9 51
Help me. 3 54
fso.FolderExists("\\server\HiddenFolder$") 4 65
Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question