Solved

Inserting a Text File as Code

Posted on 1998-08-05
5
174 Views
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 4prd@qlink.queensu.ca
0
Comment
Question by:2helpornot2help
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:DQ
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.

0
 

Author Comment

by:2helpornot2help
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??
0
 
LVL 14

Expert Comment

by:waty
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

0
 
LVL 14

Expert Comment

by:waty
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.
0
 
LVL 1

Accepted Solution

by:
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

fWriteIniErrHandler:
    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

'Function
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

fGetFromIniErrHandler:
   fGetFromIni = "ERROR"

End Function




0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

732 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