Solved

Inserting a Text File as Code

Posted on 1998-08-05
5
173 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
  • 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we 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…
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…

830 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