Read Connection String From .INI File - vb6

I have a .ini file setup from which I read connection strings. I have a modeule to read the strings:
Option Explicit
Private 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 Const iniPath = "\DBSettings.INI"
Public Sub Main()
Dim dbPath As String
Dim dbPath As String
dbPath = GetSetting("DataBase", "DBPath")
dbPath= GetSetting("DataBase", "DBPath")
Form1.Show
End Sub
Private Function GetSetting(ByVal pHeading As String, ByVal pKey As String) As String
Const cparmLen = 100
Dim sReturn As String * cparmLen
Dim sDefault As String * cparmLen
Dim aLength As Long
aLength = GetPrivateProfileString(pHeading, pKey _
, sDefault, sReturn, cparmLen, App.Path & iniPath)
GetSetting = Mid(sReturn, 1, aLength)
End Function
Now, I am trying to display the strings on click of a button:
Option Explicit
Public Sub Command1_Click()
MsgBox (dbPath)
MsgBox (dbPath)
End Sub
However, it seems the form cannot see the variables in the module. How may I fix this?
Any help would be appreciated.

dbPath is declared in a sub so scoping rules state that the variable only exists within that sub.
To make a variable visible everywhere declare it in a module with the public access modifier:
public dbPath as string
And remove the dim in main()

DBPath is a Procedure Scope variable, as it is declared inside a Procedure (Sub Main()).
It will not be visible from other modules, or even the module itself.
And also, you cannot declare a Public variable inside a Procedure (Sub or Function).
For further info regarding variable scope:
http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx

Related

How to write .ini Format from a Flexgrid in VB6

What i want :
to a make a button that exports the selected data in this FelxGrid into .ini file format take (heights) as section and the others as values and keys , it dosen't have to be ini format it can also be something close to it
What i tried so far
Private Sub commandbutton_1()
Dim configfile As String
Dim myArray() As String 'i tried using arry but it didn't work
configfile = "C:\" & "\CONFIGMEEE!.INI"
PutINISetting "", "", SettingsGrid.Clip, configfile
MsgBox "Exported.", vbInformation, "Settings"
SettingsGrid.SaveGrid configfile, flexFileTabText
What happened then
the data we exported but not formatted at all as ini and written so :
Important to know
The flexgrid iam using is a vsflexgrid not an msflexgrid
And i am also using this as a MOUDLE
'API Function to write information to the INI File
Private Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
'Get the INI Setting from the File
Public Function GetINISetting(ByVal sHeading As String, _
ByVal sKey As String, _
sINIFileName) As String
Const cparmLen = 50
Dim sReturn As String * cparmLen
Dim sDefault As String * cparmLen
Dim lLength As Long
lLength = GetPrivateProfileString(sHeading, _
sKey, _
sDefault, _
sReturn, _
cparmLen, _
sINIFileName)
GetINISetting = Mid(sReturn, 1, lLength)
End Function
'Save INI Setting in the File
Public Function PutINISetting(ByVal sHeading As String, _
ByVal sKey As String, _
ByVal sSetting As String, _
sINIFileName) As Boolean
Const cparmLen = 50
Dim sReturn As String * cparmLen
Dim sDefault As String * cparmLen
Dim aLength As Long
aLength = WritePrivateProfileString(sHeading, _
sKey, _
sSetting, _
sINIFileName)
PutINISetting = True
End Function

VB6 - How to store URL contents in string and save as file(ex .txt)

Option Explicit
Public Sub save_url_contents_as_text()
Dim MyUrl As String
Dim MyFile As Object 'store the text file here
Dim tempstring As String
MyUrl = "www.AnythingIWantToPutHere"
'I want your help here. Something Like a function
tempstring = geturltext(MyUrl)
'I want to save the URL text contents here
MyFile = tempstring
End Sub
Taken directly from here. Change the URL and the filename as desired. This call writes the contents of the URL to a disk file.
This example shows how to use the URLDownloadToFile API function to download a file from a URL into a file in Visual Basic 6.
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Sub Form_Load()
' Download the file.
URLDownloadToFile 0, _
"http://www.vb-helper.com/vbhelper_425_64.gif", _
"C:\vbhelper_425_64.gif", 0, 0
End Sub

How to find path of the user profile in VBA

How can I get the path of the current user that is as same as %UserProfile% result in Windows 7?
Or, quite simply:
MsgBox Environ$("USERPROFILE")
You can use the Environ() method in VBA to expand environmental variables, just pass the argument as a string and drop the % markers.
I use this function:
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
' Return the user's profile path.
Private Function ProfilePath() As String
Dim win_dir As String
Dim user_name As String
Dim ret_len As Long
' Get the windows directory.
win_dir = Space$(256)
ret_len = GetWindowsDirectory(win_dir, Len(win_dir))
win_dir = Left$(win_dir, ret_len)
' Get the user's name.
user_name = Space$(256)
GetUserName user_name, ret_len
user_name = Left$(user_name, ret_len)
If (Asc(Right$(user_name, 1)) = 0) Then
user_name = Left$(user_name, Len(user_name) - 1)
End If
ProfilePath = win_dir & "\" & user_name
End Function

Automatically download a list of images and name them

I have a xls/csv list of images:
Name image url
test.jpg http://test.com/232dd.jpg
test2.jpg http://test.com/2390j.jpg
I have about 200 of these...is there a way to download the list and name them as identified in the xls file?
Here is my Excel VBA:
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Sub download_pics()
Dim rng As Range
Dim cell As Variant
Set rng = Range("A1:B10")
For Each cell In rng
' Download the file.
URLDownloadToFile 0, cell(rng, 2).Value, "C:\" & cell(rng, 1).Value, 0, 0
Next
End Sub
Running into type mismatch error with URLDownloadToFile
OK The type mismatch has to do with your iteration. You need to specify how you're iterating over rng with the For each cell in rng statement, like:
For each cell in rng.Rows
Otherwise, it treats that statement as For each cell in rng.Cells and that raises the mismatch error.
I made some modifications to the code (based on Sid's answer here), so it checks to ensure the file downloaded successfully, but mostly what you found was correct, you just need to implement it a little differently.
Private Declare Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Sub download_pics()
Dim rng As Range
Dim myRow As Range
Dim imgName As String
Dim fileLocation As String
Dim Ret As Long 'return value from the URLDownloadToFile function
Set rng = Range("A1:B10")
For Each myRow In rng.Rows
With myRow
imgName = .Columns(2).Value
fileLocation = "C:\" & .Columns(1).Value
With .Columns(1).Offset(, 2)
If URLDownloadToFile(0, imgName, fileLocation, 0, 0) = 0 Then
.Value = "downloaded successfully"
Else:
.Value = "download failed!"
End If
End With
End With
Next
End Sub

vb6 - how to get the remote computer name based on the given IP address

how can i get the remote computer name based on a given IP Address in vb6? Is there any way that i can list out a list of computers linked to current computer?
If reverse DNS lookup does what you want this might help. This example simplifies the processing of the DNS results, but should get you started and may be enough:
Option Explicit
Private Const DNS_TYPE_PTR = &HC
Private Const DNS_QUERY_STANDARD = &H0
Private Const DnsFreeRecordListDeep = 1&
Private Enum DNS_STATUS
ERROR_BAD_IP_FORMAT = -3&
ERROR_NO_PTR_RETURNED = -2&
ERROR_NO_RR_RETURNED = -1&
DNS_STATUS_SUCCESS = 0&
End Enum
Private Type VBDnsRecord
pNext As Long
pName As Long
wType As Integer
wDataLength As Integer
Flags As Long
dwTTL As Long
dwReserved As Long
prt As Long
others(9) As Long
End Type
Private Declare Function DnsQuery Lib "Dnsapi" Alias "DnsQuery_A" ( _
ByVal Name As String, _
ByVal wType As Integer, _
ByVal Options As Long, _
ByRef aipServers As Any, _
ByRef ppQueryResultsSet As Long, _
ByVal pReserved As Long) As Long
Private Declare Function DnsRecordListFree Lib "Dnsapi" ( _
ByVal pDnsRecord As Long, _
ByVal DnsFreeRecordListDeep As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByRef pTo As Any, _
ByRef uFrom As Any, _
ByVal lSize As Long)
Private Declare Function StrCopyA Lib "kernel32" Alias "lstrcpyA" ( _
ByVal retval As String, _
ByVal PTR As Long) As Long
Private Declare Function StrLenA Lib "kernel32" Alias "lstrlenA" ( _
ByVal PTR As Long) As Long
Public Function IP2HostName(ByVal IP As String, ByRef HostName As String) As Long
Dim Octets() As String
Dim OctX As Long
Dim NumPart As Long
Dim BadIP As Boolean
Dim lngDNSRec As Long
Dim Record As VBDnsRecord
Dim Length As Long
'Returns DNS_STATUS Enum values, otherwise a DNS system error code.
IP = Trim$(IP)
If Len(IP) = 0 Then IP2HostName = ERROR_BAD_IP_FORMAT: Exit Function
Octets = Split(IP, ".")
If UBound(Octets) <> 3 Then IP2HostName = ERROR_BAD_IP_FORMAT: Exit Function
For OctX = 0 To 3
If IsNumeric(Octets(OctX)) Then
NumPart = CInt(Octets(OctX))
If 0 <= NumPart And NumPart <= 255 Then
Octets(OctX) = CStr(NumPart)
Else
BadIP = True
Exit For
End If
Else
BadIP = True
Exit For
End If
Next
If BadIP Then IP2HostName = ERROR_BAD_IP_FORMAT: Exit Function
IP = Octets(3) & "." & Octets(2) & "." & Octets(1) & "." & Octets(0) & ".IN-ADDR.ARPA"
IP2HostName = DnsQuery(IP, DNS_TYPE_PTR, DNS_QUERY_STANDARD, ByVal 0, lngDNSRec, 0)
If IP2HostName = DNS_STATUS_SUCCESS Then
If lngDNSRec <> 0 Then
CopyMemory Record, ByVal lngDNSRec, LenB(Record)
With Record
If .wType = DNS_TYPE_PTR Then
Length = StrLenA(.prt)
HostName = String$(Length, 0)
StrCopyA HostName, .prt
Else
IP2HostName = ERROR_NO_PTR_RETURNED
End If
End With
DnsRecordListFree lngDNSRec, DnsFreeRecordListDeep
Else
IP2HostName = ERROR_NO_RR_RETURNED
End If
'Else
'Return with DNS error code.
End If
End Function
Note however it does not handle NetBIOS names.
According to this Microsoft support article, the standard GetHostByAddr() functions should do it. Unfortunately, I can't find any examples of how to do a GetHostByAddr call in VB6, but perhaps someone else can help with that part. Alternately, you could run a commandline tool like nslookup:
bensonk#hunter ~/Desktop/cont $ nslookup 64.34.119.12
Server: 208.67.222.222
Address: 208.67.222.222#53
Non-authoritative answer:
12.119.34.64.in-addr.arpa name = stackoverflow.com.
That example was run on a linux machine, but the same command will work fine on windows.
MOST SIMPLE TECHNIQUE EVER
To send computer name ,
Do this:
Create a textbox,
Change its multiline property to true.
After that, in the text property of that textbox, write this:
echo %computername% >> C:\temp.txt
then, using Fileinput, input the textfile and input the textfile C:\temp.txt.
If you want to use this for sending over winsock or LAN, Send the text box text using winsock1.sendata
Done

Resources