Solved

Inserting a Text File as Code

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now