Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 12-17-2018, 03:23 AM   #1
aman
Newly Registered User
 
Join Date: Oct 2008
Posts: 1,250
Thanks: 54
Thanked 3 Times in 2 Posts
aman is an unknown quantity at this point
Using Ini files to create text file (with various sections on it ) to store values

Hi Guys,

I am trying to use ini files which will act as a mini database for outlook. So Outlook/VBA code will create a text file (ini file) and data is stored in a text file so everytime you need to change the data then it doesn't require changing the code . The code works fine. The highlighted lines will store data in a listbox eventually and my question is if the listbox is 2 column listbox then how can we change those lines as below:

e.g
1,aaa
2,bbb
3,ccc
4,ddd
5,eee
6,fff
7,ggg

00,aaaa
11,bbbb
22,cccc

etc...



Code:
Const IniFileName As String = "u:\test.ini"
Const ThisVersion As String = "1.1"
' the path and filename to the file containing the information you want to read/write
Private Declare Function GetPrivateProfileStringA Lib "Kernel32" (ByVal strSection As String, _
   ByVal strKey As String, ByVal strDefault As String, ByVal strReturnedString As String, _
   ByVal lngSize As Long, ByVal strFileNameName As String) As Long
  
Private Declare Function WritePrivateProfileStringA Lib "Kernel32" (ByVal strSection As String, _
   ByVal strKey As String, ByVal strString As String, ByVal strFileNameName As String) As Long


Sub SaveIni()


' saves information in the file IniFileName
    If Not WritePrivateProfileString32(IniFileName, "VersionInfo", "Version", "1.0") Then
        MsgBox "Not able to save user info in " & IniFileName, vbExclamation, "Folder does not exist!"
        Exit Sub
    End If
    
    WritePrivateProfileString32 IniFileName, "VersionInfo", "VersionErrorText", "You are not running the correct version of this Software. Please contact Joe Bloggs or Jane Doe"


   WritePrivateProfileString32 IniFileName, "Indexes", "PreCompletion", "aaa|bbb|ccc|ddd|eee|fff|ggg"
   WritePrivateProfileString32 IniFileName, "Indexes", "PostCompletion", "aaaa|bbbb|cccc|dddd|eeee|ffff|gggg"
   WritePrivateProfileString32 IniFileName, "Indexes", "MortgageServices", "111|222|333|444|555|666|777|888|999|000"


End Sub
Private Function WritePrivateProfileString32(ByVal strFileName As String, ByVal strSection As String, ByVal strKey As String, ByVal strValue As String) As Boolean
Dim lngValid As Long
    On Error Resume Next
    lngValid = WritePrivateProfileStringA(strSection, strKey, strValue, strFileName)
    If lngValid > 0 Then WritePrivateProfileString32 = True
    On Error GoTo 0
End Function


Private Function GetPrivateProfileString32(ByVal strFileName As String, ByVal strSection As String, ByVal strKey As String, Optional strDefault) As String
Dim strReturnString As String, lngSize As Long, lngValid As Long
    On Error Resume Next
    If IsMissing(strDefault) Then strDefault = ""
    strReturnString = Space(2048)
    lngSize = Len(strReturnString)
    lngValid = GetPrivateProfileStringA(strSection, strKey, strDefault, strReturnString, lngSize, strFileName)
    GetPrivateProfileString32 = Left(strReturnString, lngValid)
'   On Error GoTo 0
End Function

aman is offline   Reply With Quote
Old 12-17-2018, 04:05 AM   #2
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,569
Thanks: 68
Thanked 2,745 Times in 2,630 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Using Ini files to create text file (with various sections on it ) to store value

first you must save your list items in ini file delimited with comma and pipe:
Code:
...
   WritePrivateProfileString32 IniFileName, "Indexes", "PreCompletion", "1,aaa|2,bbb|3,ccc|4,ddd|5,eee|6,fff|7,ggg"
create a public function that will parse this data:
Code:
Public Function fnFillList(l As ListBox)
Dim m As String
Dim vList As Variant
Dim v As Variant
m = GetPrivateProfileString32("E:\test.ini", "Indexes", "PreCompletion")
vList = Split(m, "|")
For Each v In vList
    l.AddItem v
Next
End Function
create a Listbox in a Form.
on it's property->data->row source type: value list
on it's property->format->column count: 2

on the form's Load Event, fill the listbox:
Code:
Private Sub Form_Load()
Call fnFillList(Me.List2)
End Sub
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Old 12-17-2018, 04:53 AM   #3
aman
Newly Registered User
 
Join Date: Oct 2008
Posts: 1,250
Thanks: 54
Thanked 3 Times in 2 Posts
aman is an unknown quantity at this point
Re: Using Ini files to create text file (with various sections on it ) to store value

Thanks Arnelp, I tried it but nothing got populated in the listbox.

When I step through the code then I can see the values get stored in variable 'v' but nothing shows in the listbox :
Code:
Public Function fnFillList(l As ListBox)
Dim m As String
Dim vList As Variant
Dim v As Variant
m = GetPrivateProfileString32("U:\test.ini", "Indexes", "PreCompletion")
vList = Split(m, "|")
For Each v In vList
    l.AddItem v
Next
End Function

aman is offline   Reply With Quote
Old 12-17-2018, 04:58 AM   #4
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,569
Thanks: 68
Thanked 2,745 Times in 2,630 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Using Ini files to create text file (with various sections on it ) to store value

again plz review the steps. i already tested it and it worked for me.

__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Store Unbound Text values into Table Solly Forms 2 03-10-2016 07:42 AM
Store lookup values based on BETWEEN text field name maw230 Tables 2 01-21-2016 11:26 AM
How do you store multiple file locations in a text box chuckcoleman Forms 3 12-16-2011 08:50 AM
store images in text field, but use file picker waza Forms 1 03-12-2008 03:47 PM
How to create a folder to store exported text file? marco100 Macros 1 10-12-2002 09:14 PM




All times are GMT -8. The time now is 12:23 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World