NBVC
Well-known Member
- Joined
- Aug 31, 2005
- Messages
- 5,828
Hello all,
I am trying to use VBA to automate a query to my ERP database system.
This first code is successfull at doing that but it requires the database, user id and password to be part of the macro. The macro will be used by many so I tried to set those variables through use of input boxes. See second code below. It accepts the inputs, but does not retrieve the data.
Can anyone see what may be causing this problem?
It looks like it is trying to use the actual variable names instead of the assigned strings. How do I get it to reference the assigned database, user id, password, instead of the actual variable names?
I am trying to use VBA to automate a query to my ERP database system.
This first code is successfull at doing that but it requires the database, user id and password to be part of the macro. The macro will be used by many so I tried to set those variables through use of input boxes. See second code below. It accepts the inputs, but does not retrieve the data.
Can anyone see what may be causing this problem?
It looks like it is trying to use the actual variable names instead of the assigned strings. How do I get it to reference the assigned database, user id, password, instead of the actual variable names?
Code:
Sub GetAllQueries1()
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DSN=SANDBOX_ODBC;UID=NBVC;Pwd=ABC;DBQ=SANDBOX;DBA=W;APA=T;EXC=F;FEN=T;QTO=T;FRC=10;FDL=10;LOB=T;RST=T;GDE=F;FRL=F;BAM=IfAllSuccess" _
), Array("ful;MTS=F;MDI=F;CSR=F;FWC=F;PFC=10;TLO=0;")), Destination:=Range( _
"A1"))
.CommandText = Array( _
"SELECT PART.ID, PART.FABRICATED" & Chr(13) & "" & Chr(10) & "FROM SYSADM.PART PART" & Chr(13) & "" & Chr(10) & "WHERE (PART.FABRICATED='Y') AND (PART.QTY_ON_HAND>100)" _
)
.Name = "Query from SANDBOX_ODBC"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
Code:
Sub GetAllQueries()
Dim User As Variant, Password As Variant, database As Variant
database = InputBox("Database?", "Database")
ODBC = database & "_ODBC"
User = InputBox("User ID?", "UserId")
Password = InputBox("Password", "Password")
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DSN=ODBC;UID=user;Pwd=password;DBQ=database ;DBA=W;APA=T;EXC=F;FEN=T;QTO=T;FRC=10;FDL=10;LOB=T;RST=T;GDE=F;FRL=F;BAM=IfAllSuccess" _
), Array("ful;MTS=F;MDI=F;CSR=F;FWC=F;PFC=10;TLO=0;")), Destination:=Range( _
"A1"))
.CommandText = Array( _
"SELECT PART.ID, PART.FABRICATED" & Chr(13) & "" & Chr(10) & "FROM SYSADM.PART PART" & Chr(13) & "" & Chr(10) & "WHERE (PART.FABRICATED='Y') AND (PART.QTY_ON_HAND>100)" _
)
.Name = "Query from" & ODBC
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub